File:  [gforth] / gforth / cross.fs
Revision 1.76: download - view: text, annotated - select for diffs
Mon May 17 13:12:25 1999 UTC (20 years, 6 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
Small fix

    1: \ CROSS.FS     The Cross-Compiler                      06oct92py
    2: \ Idea and implementation: Bernd Paysan (py)
    3: 
    4: \ Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
    5: 
    6: \ This file is part of Gforth.
    7: 
    8: \ Gforth is free software; you can redistribute it and/or
    9: \ modify it under the terms of the GNU General Public License
   10: \ as published by the Free Software Foundation; either version 2
   11: \ of the License, or (at your option) any later version.
   12: 
   13: \ This program is distributed in the hope that it will be useful,
   14: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: \ GNU General Public License for more details.
   17: 
   18: \ You should have received a copy of the GNU General Public License
   19: \ along with this program; if not, write to the Free Software
   20: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   21: 
   22: 0 
   23: [IF]
   24: 
   25: ToDo:
   26: Crossdoc destination ./doc/crossdoc.fd makes no sense when
   27: cross.fs is uses seperately. jaw
   28: Do we need this char translation with >address and in branchoffset? 
   29: (>body also affected) jaw
   30: Clean up mark> and >resolve stuff jaw
   31: 
   32: [THEN]
   33: 
   34: 
   35: hex     \ the defualt base for the cross-compiler is hex !!
   36: Warnings off
   37: 
   38: \ words that are generaly useful
   39: 
   40: : KB  400 * ;
   41: : >wordlist ( vocabulary-xt -- wordlist-struct )
   42:   also execute get-order swap >r 1- set-order r> ;
   43: 
   44: : umax 2dup u< IF swap THEN drop ;
   45: : umin 2dup u> IF swap THEN drop ;
   46: 
   47: : string, ( c-addr u -- )
   48:     \ puts down string as cstring
   49:     dup c, here swap chars dup allot move ;
   50: 
   51: : SetValue ( n -- <name> )
   52: \G Same behaviour as "Value" if the <name> is not defined
   53: \G Same behaviour as "to" if <name> is defined
   54: \G SetValue searches in the current vocabulary
   55:   save-input bl word >r restore-input throw r> count
   56:   get-current search-wordlist
   57:   IF	drop >r
   58: 	\ we have to set current to be topmost context wordlist
   59: 	get-order get-order get-current swap 1+ set-order
   60: 	r> ['] to execute
   61: 	set-order
   62:   ELSE Value THEN ;
   63: 
   64: : DefaultValue ( n -- <name> )
   65: \G Same behaviour as "Value" if the <name> is not defined
   66: \G DefaultValue searches in the current vocabulary
   67:  save-input bl word >r restore-input throw r> count
   68:  get-current search-wordlist
   69:  IF bl word drop 2drop ELSE Value THEN ;
   70: 
   71: hex
   72: 
   73: Vocabulary Cross
   74: Vocabulary Target
   75: Vocabulary Ghosts
   76: VOCABULARY Minimal
   77: only Forth also Target also also
   78: definitions Forth
   79: 
   80: : T  previous Cross also Target ; immediate
   81: : G  Ghosts ; immediate
   82: : H  previous Forth also Cross ; immediate
   83: 
   84: forth definitions
   85: 
   86: : T  previous Cross also Target ; immediate
   87: : G  Ghosts ; immediate
   88: 
   89: : >cross  also Cross definitions previous ;
   90: : >target also Target definitions previous ;
   91: : >minimal also Minimal definitions previous ;
   92: 
   93: H
   94: 
   95: >CROSS
   96: 
   97: \ 1 Constant Cross-Flag	\ to check whether assembler compiler plug-ins are
   98: 			\ for cross-compiling
   99: \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
  100: 
  101: : comment? ( c-addr u -- c-addr u )
  102:         2dup s" (" compare 0=
  103:         IF    postpone (
  104:         ELSE  2dup s" \" compare 0= IF postpone \ THEN
  105:         THEN ;
  106: 
  107: \ Begin CROSS COMPILER:
  108: 
  109: 
  110: 
  111: \ \ --------------------        Error Handling                  05aug97jaw
  112: 
  113: \ Flags
  114: 
  115: also forth definitions  \ these values may be predefined before
  116:                         \ the cross-compiler is loaded
  117: 
  118: false DefaultValue stack-warn   	 \ check on empty stack at any definition
  119: false DefaultValue create-forward-warn   \ warn on forward declaration of created words
  120: 
  121: previous >CROSS
  122: 
  123: : .dec
  124:   base @ decimal swap . base ! ;
  125: 
  126: : .sourcepos
  127:   cr sourcefilename type ." :"
  128:   sourceline# .dec ;
  129: 
  130: : warnhead
  131: \G display error-message head
  132: \G perhaps with linenumber and filename
  133:   .sourcepos ." Warning: " ;
  134: 
  135: : empty? depth IF .sourcepos ." Stack not empty!"  THEN ;
  136: 
  137: stack-warn [IF]
  138: : defempty? empty? ;
  139: [ELSE]
  140: : defempty? ; immediate
  141: [THEN]
  142: 
  143: \ debugging
  144: 
  145: 0 [IF]
  146: 
  147: This implements debugflags for the cross compiler and the compiled
  148: images. It works identical to the has-flags in the environment.
  149: The debugflags are defined in a vocabluary. If the word exists and
  150: its value is true, the flag is switched on.
  151: 
  152: [THEN]
  153: 
  154: Vocabulary debugflags	\ debug flags for cross
  155: also debugflags get-order over
  156: Constant debugflags-wl
  157: set-order previous
  158: 
  159: : DebugFlag
  160:   get-current >r debugflags-wl set-current
  161:   SetValue
  162:   r> set-current ;
  163: 
  164: : Debug? ( adr u -- flag )
  165: \G return true if debug flag is defined or switched on
  166:   debugflags-wl search-wordlist
  167:   IF EXECUTE
  168:   ELSE false THEN ;
  169: 
  170: : D? ( <name> -- flag )
  171: \G return true if debug flag is defined or switched on
  172: \G while compiling we do not return the current value but
  173:   bl word count debug? ;
  174: 
  175: : [d?]
  176: \G compile the value-xt so the debug flag can be switched
  177: \G the flag must exist!
  178:   bl word count debugflags-wl search-wordlist
  179:   IF 	compile,
  180:   ELSE  -1 ABORT" unknown debug flag"
  181: 	\ POSTPONE false 
  182:   THEN ; immediate
  183: 
  184: \ \ GhostNames Ghosts                                  9may93jaw
  185: 
  186: \ second name source to search trough list
  187: 
  188: VARIABLE GhostNames
  189: 0 GhostNames !
  190: 
  191: : GhostName ( -- addr )
  192:     here GhostNames @ , GhostNames ! here 0 ,
  193:     bl word count
  194:     \ 2dup type space
  195:     string, \ !! cfalign ?
  196:     align ;
  197: 
  198: \ Ghost Builder                                        06oct92py
  199: 
  200: \ <T T> new version with temp variable                 10may93jaw
  201: 
  202: VARIABLE VocTemp
  203: 
  204: : <T  get-current VocTemp ! also Ghosts definitions ;
  205: : T>  previous VocTemp @ set-current ;
  206: 
  207: hex
  208: 4711 Constant <fwd>             4712 Constant <res>
  209: 4713 Constant <imm>             4714 Constant <do:>
  210: 4715 Constant <skip>
  211: 
  212: \ iForth makes only immediate directly after create
  213: \ make atonce trick! ?
  214: 
  215: Variable atonce atonce off
  216: 
  217: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
  218: 
  219: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  220: 
  221: : >magic ;		\ type of ghost
  222: : >link cell+ ;		\ pointer where ghost is in target, or if unresolved
  223: 			\ points to the where we have to resolve (linked-list)
  224: : >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
  225: : >end 3 cells + ;	\ room for additional tags
  226: 			\ for builder (create, variable...) words the
  227: 			\ execution symantics of words built are placed here
  228: 
  229: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
  230: Variable last-ghost	\ last ghost that is created
  231: Variable last-header-ghost \ last ghost definitions with header
  232: 
  233: : Make-Ghost ( "name" -- ghost )
  234:   >in @ GhostName swap >in !
  235:   <T Create atonce @ IF immediate atonce off THEN
  236:   here tuck swap ! ghostheader T>
  237:   dup last-ghost !
  238:   DOES> dup executed-ghost ! >exec @ execute ;
  239: 
  240: \ ghost words                                          14oct92py
  241: \                                          changed:    10may93py/jaw
  242: 
  243: : gfind   ( string -- ghost true/1 / string false )
  244: \ searches for string in word-list ghosts
  245:   dup count [ ' ghosts >wordlist ] ALiteral search-wordlist
  246:   dup IF >r >body nip r>  THEN ;
  247: 
  248: : gdiscover ( xt -- ghost true | xt false )
  249:   GhostNames
  250:   BEGIN @ dup
  251:   WHILE 2dup
  252:         cell+ @ dup >magic @ <fwd> <>
  253:         >r >link @ = r> and
  254:         IF cell+ @ nip true EXIT THEN
  255:   REPEAT
  256:   drop false ;
  257: 
  258: VARIABLE Already
  259: 
  260: : ghost   ( "name" -- ghost )
  261:   Already off
  262:   >in @  bl word gfind   IF  atonce off Already on nip EXIT  THEN
  263:   drop  >in !  Make-Ghost ;
  264: 
  265: : >ghostname ( ghost -- adr len )
  266:   GhostNames
  267:   BEGIN @ dup
  268:   WHILE 2dup cell+ @ =
  269:   UNTIL nip 2 cells + count
  270:   ELSE  2drop 
  271: 	\ true abort" CROSS: Ghostnames inconsistent"
  272: 	s" ?!?!?!"
  273:   THEN ;
  274: 
  275: ' >ghostname ALIAS @name
  276: 
  277: : forward? ( ghost -- flag )
  278:   >magic @ <fwd> = ;
  279: 
  280: : undefined? ( ghost -- flag )
  281:   >magic @ dup <fwd> = swap <skip> = or ;
  282: 
  283: \ Predefined ghosts                                    12dec92py
  284: 
  285: ghost 0=                                        drop
  286: ghost branch    ghost ?branch                   2drop
  287: ghost (do)      ghost (?do)                     2drop
  288: ghost (for)                                     drop
  289: ghost (loop)    ghost (+loop)                   2drop
  290: ghost (next)                                    drop
  291: ghost unloop    ghost ;S                        2drop
  292: ghost lit       ghost (compile) ghost !         2drop drop
  293: ghost (does>)   ghost noop                      2drop
  294: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  295: ghost '                                         drop
  296: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  297: ghost :dovar					drop
  298: ghost over      ghost =         ghost drop      2drop drop
  299: ghost - drop
  300: ghost 2drop drop
  301: ghost 2dup drop
  302: 
  303: \ \ Parameter for target systems                         06oct92py
  304: 
  305: \ we define it ans like...
  306: wordlist Constant target-environment
  307: 
  308: VARIABLE env-current \ save information of current dictionary to restore with environ>
  309: 
  310: : >ENVIRON get-current env-current ! target-environment set-current ;
  311: : ENVIRON> env-current @ set-current ; 
  312: 
  313: >TARGET
  314: 
  315: : environment? ( adr len -- [ x ] true | false )
  316:   target-environment search-wordlist 
  317:   IF execute true ELSE false THEN ;
  318: 
  319: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
  320: 
  321: : has? 	bl word count T environment? H 
  322: 	IF 	\ environment variable is present, return its value
  323: 	ELSE	\ environment variable is not present, return false
  324: 		false \ debug true ABORT" arg" 
  325: 	THEN ;
  326: 
  327: : $has? T environment? H IF ELSE false THEN ;
  328: 
  329: >ENVIRON get-order get-current swap 1+ set-order
  330: true SetValue compiler
  331: true  SetValue cross
  332: true SetValue standard-threading
  333: >TARGET previous
  334: 
  335: 
  336: mach-file count included hex
  337: 
  338: >ENVIRON
  339: 
  340: T has? ec H
  341: [IF]
  342: false DefaultValue relocate
  343: false DefaultValue file
  344: false DefaultValue OS
  345: false DefaultValue prims
  346: false DefaultValue floating
  347: false DefaultValue glocals
  348: false DefaultValue dcomps
  349: false DefaultValue hash
  350: false DefaultValue xconds
  351: false DefaultValue header
  352: [THEN]
  353: 
  354: true DefaultValue interpreter
  355: true DefaultValue ITC
  356: false DefaultValue rom
  357: true DefaultValue standardthreading
  358: 
  359: >TARGET
  360: s" relocate" T environment? H 
  361: [IF]	SetValue NIL
  362: [ELSE]	>ENVIRON T NIL H SetValue relocate
  363: [THEN]
  364: 
  365: >CROSS
  366: 
  367: \ \ Create additional parameters                         19jan95py
  368: 
  369: \ currently cross only works for host machines with address-unit-bits
  370: \ eual to 8 because of s! and sc!
  371: \ but I start to query the environment just to modularize a little bit
  372: 
  373: : check-address-unit-bits ( -- )	
  374: \	s" ADDRESS-UNIT-BITS" environment?
  375: \	IF 8 <> ELSE true THEN
  376: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
  377: 
  378: \	shit, this doesn't work because environment? is only defined for 
  379: \	gforth.fi and not kernl???.fi
  380: 	;
  381: 
  382: check-address-unit-bits
  383: 8 Constant bits/byte	\ we define: byte is address-unit
  384: 
  385: 1 bits/byte lshift Constant maxbyte 
  386: \ this sets byte size for the target machine, an (probably right guess) jaw
  387: 
  388: T
  389: NIL		   	Constant TNIL
  390: cell               	Constant tcell
  391: cell<<             	Constant tcell<<
  392: cell>bit           	Constant tcell>bit
  393: bits/char          	Constant tbits/char
  394: bits/char H bits/byte T /      
  395: 			Constant tchar
  396: float             	Constant tfloat
  397: 1 bits/char lshift 	Constant tmaxchar
  398: [IFUNDEF] bits/byte
  399: 8			Constant tbits/byte
  400: [ELSE]
  401: bits/byte		Constant tbits/byte
  402: [THEN]
  403: H
  404: tbits/byte bits/byte /	Constant tbyte
  405: 
  406: 
  407: \ Variables                                            06oct92py
  408: 
  409: Variable image
  410: Variable tlast    TNIL tlast !  \ Last name field
  411: Variable tlastcfa \ Last code field
  412: Variable tdoes    \ Resolve does> calls
  413: Variable bit$
  414: 
  415: \ statistics						10jun97jaw
  416: 
  417: Variable headers-named 0 headers-named !
  418: Variable user-vars 0 user-vars !
  419: 
  420: : target>bitmask-size ( u1 -- u2 )
  421:   1- tcell>bit rshift 1+ ;
  422: 
  423: : allocatetarget ( size --- adr )
  424:   dup allocate ABORT" CROSS: No memory for target"
  425:   swap over swap erase ;
  426: 
  427: \ \ memregion.fs
  428: 
  429: 
  430: Variable last-defined-region    \ pointer to last defined region
  431: Variable region-link            \ linked list with all regions
  432: Variable mirrored-link          \ linked list for mirrored regions
  433: 0 dup mirrored-link ! region-link !
  434: 
  435: 
  436: : >rname 6 cells + ;
  437: : >rbm   5 cells + ;
  438: : >rmem  4 cells + ;
  439: : >rlink 3 cells + ;
  440: : >rdp 2 cells + ;
  441: : >rlen cell+ ;
  442: : >rstart ;
  443: 
  444: 
  445: : region ( addr len -- )                \G create a new region
  446:   \ check whether predefined region exists 
  447:   save-input bl word find >r >r restore-input throw r> r> 0= 
  448:   IF	\ make region
  449: 	drop
  450: 	save-input create restore-input throw
  451: 	here last-defined-region !
  452: 	over ( startaddr ) , ( length ) , ( dp ) ,
  453: 	region-link linked 0 , 0 , bl word count string,
  454:   ELSE	\ store new parameters in region
  455:         bl word drop
  456: 	>body >r r@ last-defined-region !
  457: 	r@ >rlen ! dup r@ >rstart ! r> >rdp !
  458:   THEN ;
  459: 
  460: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  461:   dup >rstart @ swap >rlen @ over + ;
  462: 
  463: : extent  ( region -- startaddr len )   \G returns the really used area
  464:   dup >rstart @ swap >rdp @ over - ;
  465: 
  466: : area ( region -- startaddr totallen ) \G returns the total area
  467:   dup >rstart swap >rlen @ ;
  468: 
  469: : mirrored                              \G mark a region as mirrored
  470:   mirrored-link
  471:   align linked last-defined-region @ , ;
  472: 
  473: : .addr ( u -- )
  474: \G prints a 16 or 32 Bit nice hex value
  475:   base @ >r hex
  476:   tcell 2 u>
  477:   IF s>d <# # # # # '. hold # # # # #> type
  478:   ELSE s>d <# # # # # # #> type
  479:   THEN r> base ! ;
  480: 
  481: : .regions                      \G display region statistic
  482: 
  483:   \ we want to list the regions in the right order
  484:   \ so first collect all regions on stack
  485:   0 region-link @
  486:   BEGIN dup WHILE dup @ REPEAT drop
  487:   BEGIN dup
  488:   WHILE cr
  489:         0 >rlink - >r
  490:         r@ >rname count tuck type
  491:         12 swap - 0 max spaces space
  492:         ." Start: " r@ >rstart @ dup .addr space
  493:         ." End: " r@ >rlen @ + .addr space
  494:         ." DP: " r> >rdp @ .addr
  495:   REPEAT drop
  496:   s" rom" T $has? H 0= ?EXIT
  497:   cr ." Mirrored:"
  498:   mirrored-link @
  499:   BEGIN dup
  500:   WHILE space dup cell+ @ >rname count type @
  501:   REPEAT drop cr
  502:   ;
  503: 
  504: \ -------- predefined regions
  505: 
  506: 0 0 region address-space
  507: \ total memory addressed and used by the target system
  508: 
  509: 0 0 region dictionary
  510: \ rom area for the compiler
  511: 
  512: T has? rom H
  513: [IF]
  514: 0 0 region ram-dictionary mirrored
  515: \ ram area for the compiler
  516: [ELSE]
  517: ' dictionary ALIAS ram-dictionary
  518: [THEN]
  519: 
  520: 0 0 region return-stack
  521: 
  522: 0 0 region data-stack
  523: 
  524: 0 0 region tib-region
  525: 
  526: ' dictionary ALIAS rom-dictionary
  527: 
  528: 
  529: : setup-target ( -- )   \G initialize targets memory space
  530:   s" rom" T $has? H
  531:   IF  \ check for ram and rom...
  532:       \ address-space area nip 0<>
  533:       ram-dictionary area nip 0<>
  534:       rom-dictionary area nip 0<>
  535:       and 0=
  536:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
  537:   THEN
  538:   address-space area nip
  539:   IF
  540:       address-space area
  541:   ELSE
  542:       dictionary area
  543:   THEN
  544:   nip 0=
  545:   ABORT" CROSS: define at least address-space or dictionary!!"
  546: 
  547:   \ allocate target for each region
  548:   region-link
  549:   BEGIN @ dup
  550:   WHILE dup
  551:         0 >rlink - >r
  552:         r@ >rlen @
  553:         IF      \ allocate mem
  554:                 r@ >rlen @ dup
  555: 
  556:                 allocatetarget dup image !
  557:                 r@ >rmem !
  558: 
  559:                 target>bitmask-size allocatetarget
  560:                 dup bit$ !
  561:                 r> >rbm !
  562: 
  563:         ELSE    r> drop THEN
  564:    REPEAT drop ;
  565: 
  566: \ MakeKernal                                           		22feb99jaw
  567: 
  568: : makekernel ( targetsize -- targetsize )
  569:   dup dictionary >rlen ! setup-target ;
  570: 
  571: >MINIMAL
  572: : makekernel makekernel ;
  573: >CROSS
  574: 
  575: \ \ switched tdp for rom support				03jun97jaw
  576: 
  577: \ second value is here to store some maximal value for statistics
  578: \ tempdp is also embedded here but has nothing to do with rom support
  579: \ (needs switched dp)
  580: 
  581: variable tempdp	0 ,	\ temporary dp for resolving
  582: variable tempdp-save
  583: 
  584: 0 [IF]
  585: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
  586: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
  587: 
  588: \
  589: variable sramdp		\ start of ram-area for forth
  590: variable sromdp		\ start of rom-area for forth
  591: 
  592: [THEN]
  593: 
  594: 
  595: 0 value tdp
  596: variable fixed		\ flag: true: no automatic switching
  597: 			\	false: switching is done automatically
  598: 
  599: \ Switch-Policy:
  600: \
  601: \ a header is always compiled into rom
  602: \ after a created word (create and variable) compilation goes to ram
  603: \
  604: \ Be careful: If you want to make the data behind create into rom
  605: \ you have to put >rom before create!
  606: 
  607: variable constflag constflag off
  608: 
  609: : activate ( region -- )
  610: \G next code goes to this region
  611:   >rdp to tdp ;
  612: 
  613: : (switchram)
  614:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
  615:   ram-dictionary activate ;
  616: 
  617: : switchram
  618:   constflag @
  619:   IF constflag off ELSE (switchram) THEN ;
  620: 
  621: : switchrom
  622:   fixed @ ?EXIT rom-dictionary activate ;
  623: 
  624: : >tempdp ( addr -- ) 
  625:   tdp tempdp-save ! tempdp to tdp tdp ! ;
  626: : tempdp> ( -- )
  627:   tempdp-save @ to tdp ;
  628: 
  629: : >ram  fixed off (switchram) fixed on ;
  630: : >rom  fixed off switchrom fixed on ;
  631: : >auto fixed off switchrom ;
  632: 
  633: 
  634: 
  635: \ : romstart dup sromdp ! romdp ! ;
  636: \ : ramstart dup sramdp ! ramdp ! ;
  637: 
  638: \ default compilation goes to rom
  639: \ when romable support is off, only the rom switch is used (!!)
  640: >auto
  641: 
  642: : there  tdp @ ;
  643: 
  644: >TARGET
  645: 
  646: \ \ Target Memory Handling
  647: 
  648: \ Byte ordering and cell size                          06oct92py
  649: 
  650: : cell+         tcell + ;
  651: : cells         tcell<< lshift ;
  652: : chars         tchar * ;
  653: : char+		tchar + ;
  654: : floats	tfloat * ;
  655:     
  656: >CROSS
  657: : cell/         tcell<< rshift ;
  658: >TARGET
  659: 20 CONSTANT bl
  660: \ TNIL Constant NIL
  661: 
  662: >CROSS
  663: 
  664: bigendian
  665: [IF]
  666:    : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
  667:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  668:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds
  669:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  670:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
  671:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  672:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
  673:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  674: [ELSE]
  675:    : S!  ( n addr -- )  >r s>d r> tcell bounds
  676:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  677:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
  678:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  679:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
  680:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  681:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
  682:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  683: [THEN]
  684: 
  685: : taddr>region ( taddr -- region | 0 )
  686: \G finds for a target-address the correct region
  687: \G returns 0 if taddr is not in range of a target memory region
  688:   region-link
  689:   BEGIN @ dup
  690:   WHILE dup >r
  691:         0 >rlink - >r
  692:         r@ >rlen @
  693:         IF      dup r@ borders within
  694:                 IF r> r> drop nip EXIT THEN
  695:         THEN
  696:         r> drop
  697:         r>
  698:   REPEAT
  699:   2drop 0 ;
  700: 
  701: : (>regionimage) ( taddr -- 'taddr )
  702:   dup
  703:   \ find region we want to address
  704:   taddr>region dup 0= ABORT" Address out of range!"
  705:   >r
  706:   \ calculate offset in region
  707:   r@ >rstart @ -
  708:   \ add regions real address in our memory
  709:   r> >rmem @ + ;
  710: 
  711: \ Bit string manipulation                               06oct92py
  712: \                                                       9may93jaw
  713: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
  714: : bits ( n -- n ) chars Bittable + c@ ;
  715: 
  716: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
  717: : +bit ( addr n -- )  >bit over c@ or swap c! ;
  718: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
  719: 
  720: : (relon) ( taddr -- )  bit$ @ swap cell/ +bit ;
  721: : (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;
  722: 
  723: : (>image) ( taddr -- absaddr ) image @ + ;
  724: 
  725: DEFER >image
  726: DEFER relon
  727: DEFER reloff
  728: DEFER correcter
  729: 
  730: T has? relocate H
  731: [IF]
  732: ' (relon) IS relon
  733: ' (reloff) IS reloff
  734: ' (>image) IS >image
  735: [ELSE]
  736: ' drop IS relon
  737: ' drop IS reloff
  738: ' (>regionimage) IS >image
  739: [THEN]
  740: 
  741: \ Target memory access                                 06oct92py
  742: 
  743: : align+  ( taddr -- rest )
  744:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
  745: : cfalign+  ( taddr -- rest )
  746:     \ see kernel.fs:cfaligned
  747:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
  748: 
  749: >TARGET
  750: : aligned ( taddr -- ta-addr )  dup align+ + ;
  751: \ assumes cell alignment granularity (as GNU C)
  752: 
  753: : cfaligned ( taddr1 -- taddr2 )
  754:     \ see kernel.fs
  755:     dup cfalign+ + ;
  756: 
  757: : @  ( taddr -- w )     >image S@ ;
  758: : !  ( w taddr -- )     >image S! ;
  759: : c@ ( taddr -- char )  >image Sc@ ;
  760: : c! ( char taddr -- )  >image Sc! ;
  761: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
  762: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
  763: 
  764: \ Target compilation primitives                        06oct92py
  765: \ included A!                                          16may93jaw
  766: 
  767: : here  ( -- there )    there ;
  768: : allot ( n -- )        tdp +! ;
  769: : ,     ( w -- )        T here H tcell T allot  ! H T here drop H ;
  770: : c,    ( char -- )     T here    tchar allot c! H ;
  771: : align ( -- )          T here H align+ 0 ?DO  bl T c, tchar H +LOOP ;
  772: : cfalign ( -- )
  773:     T here H cfalign+ 0 ?DO  bl T c, tchar H +LOOP ;
  774: 
  775: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
  776: : A!                    swap >address swap dup relon T ! H ;
  777: : A,    ( w -- )        >address T here H relon T , H ;
  778: 
  779: >CROSS
  780: 
  781: : tcmove ( source dest len -- )
  782: \G cmove in target memory
  783:   tchar * bounds
  784:   ?DO  dup T c@ H I T c! H 1+
  785:   tchar +LOOP  drop ;
  786: 
  787: \ \ Load Assembler
  788: 
  789: >TARGET
  790: H also Forth definitions
  791: 
  792: : X 	also target bl word find
  793: 	IF	state @ IF compile,
  794: 		ELSE execute THEN
  795: 	ELSE	previous ABORT" Cross: access method not supported!"
  796: 	THEN 
  797: 	previous ; immediate
  798: 
  799: [IFDEF] asm-include asm-include [THEN] hex
  800: 
  801: previous
  802: >CROSS H
  803: 
  804: \ \ --------------------        Compiler Plug Ins               01aug97jaw
  805: 
  806: \  Compiler States
  807: 
  808: Variable comp-state
  809: 0 Constant interpreting
  810: 1 Constant compiling
  811: 2 Constant resolving
  812: 3 Constant assembling
  813: 
  814: Defer lit, ( n -- )
  815: Defer alit, ( n -- )
  816: 
  817: Defer branch, ( target-addr -- )	\ compiles a branch
  818: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
  819: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
  820: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
  821: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
  822: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
  823: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
  824: Defer branchfrom, ( -- )		\ ?!
  825: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
  826: 
  827: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
  828: Defer colonmark, ( -- addr )		\ marks a colon call
  829: Defer colon-resolve ( tcfa addr -- )
  830: 
  831: Defer addr-resolve ( target-addr addr -- )
  832: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
  833: 
  834: Defer do,	( -- do-token )
  835: Defer ?do,	( -- ?do-token )
  836: Defer for,	( -- for-token )
  837: Defer loop,	( do-token / ?do-token -- )
  838: Defer +loop,	( do-token / ?do-token -- )
  839: Defer next,	( for-token )
  840: 
  841: [IFUNDEF] ca>native
  842: defer ca>native	
  843: [THEN]
  844: 
  845: >TARGET
  846: DEFER >body             \ we need the system >body
  847: 			\ and the target >body
  848: >CROSS
  849: T 2 cells H VALUE xt>body
  850: DEFER doprim,	\ compiles start of a primitive
  851: DEFER docol,   	\ compiles start of a colon definition
  852: DEFER doer,		
  853: DEFER fini,      \ compiles end of definition ;s
  854: DEFER doeshandler,
  855: DEFER dodoes,
  856: 
  857: DEFER ]comp     \ starts compilation
  858: DEFER comp[     \ ends compilation
  859: 
  860: : (cc) T a, H ;					' (cc) IS colon,
  861: 
  862: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
  863: : (ar) T ! H ;					' (ar) IS addr-resolve
  864: : (dr)  ( ghost res-pnt target-addr addr )
  865: 	>tempdp drop over 
  866: 	dup >magic @ <do:> =
  867: 	IF 	doer,
  868: 	ELSE	dodoes,
  869: 	THEN 
  870: 	tempdp> ;				' (dr) IS doer-resolve
  871: 
  872: : (cm) ( -- addr )
  873:     T here align H
  874:     -1 colon, ;					' (cm) IS colonmark,
  875: 
  876: >TARGET
  877: : compile, colon, ;
  878: >CROSS
  879: 
  880: \ file loading
  881: 
  882: : >fl-id   1 cells + ;
  883: : >fl-name 2 cells + ;
  884: 
  885: Variable filelist 0 filelist !
  886: Create NoFile ," #load-file#"
  887: 0 Value	 filemem
  888: : loadfile  FileMem ?dup IF >fl-name ELSE NoFile THEN ;
  889: 
  890: 1 [IF] \ !! JAW WIP
  891: 
  892: : add-included-file ( adr len -- )
  893: 	dup char+ >fl-name allocate throw >r
  894: 	r@ >fl-name place
  895: 	filelist @ r@ !
  896: 	r> dup filelist ! to FileMem
  897: 	;
  898: 
  899: : included? ( c-addr u -- f )
  900:   	filelist
  901: 	BEGIN	@ dup
  902: 	WHILE	>r r@ 1 cells + count compare 0=
  903: 		IF rdrop 2drop true EXIT THEN
  904: 		r>
  905: 	REPEAT
  906: 	2drop drop false ;	
  907: 
  908: false DebugFlag showincludedfiles
  909: 
  910: : included 
  911: 	[d?] showincludedfiles
  912: 	IF	cr ." Including: " 2dup type ." ..." THEN
  913: 	FileMem >r
  914: 	2dup add-included-file included 
  915: 	r> to FileMem ;
  916: 
  917: : include bl word count included ;
  918: 
  919: : require bl word count included ;
  920: 
  921: [THEN]
  922: 
  923: \ resolve structure
  924: 
  925: : >next ;		\ link to next field
  926: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
  927: : >taddr cell+ cell+ ;	
  928: : >ghost 3 cells + ;
  929: : >file 4 cells + ;
  930: : >line 5 cells + ;
  931: 
  932: : (refered) ( ghost addr tag -- )
  933: \G creates a reference to ghost at address taddr
  934:     rot >r here r@ >link @ , r> >link ! 
  935:     ( taddr tag ) ,
  936:     ( taddr ) , 
  937:     last-header-ghost @ , 
  938:     loadfile , 
  939:     sourceline# , 
  940:   ;
  941: 
  942: : refered ( ghost tag -- )
  943: \G creates a resolve structure
  944:     T here aligned H swap (refered)
  945:   ;
  946: 
  947: : killref ( addr ghost -- )
  948: \G kills a forward reference to ghost at position addr
  949: \G this is used to eleminate a :dovar refence after making a DOES>
  950:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
  951:     swap >r >link
  952:     BEGIN dup @ dup  ( addr last this )
  953:     WHILE dup >taddr @ r@ =
  954:  	 IF   @ over !
  955: 	 ELSE nip THEN
  956:     REPEAT rdrop 2drop 
  957:   ;
  958: 
  959: Defer resolve-warning
  960: 
  961: : reswarn-test ( ghost res-struct -- ghost res-struct )
  962:   over cr ." Resolving " >ghostname type dup ."  in " >ghost @ >ghostname type ;
  963: 
  964: : reswarn-forward ( ghost res-struct -- ghost res-struct )
  965:   over warnhead >ghostname type dup ."  is referenced in " 
  966:   >ghost @ >ghostname type ;
  967: 
  968: \ ' reswarn-test IS resolve-warning
  969:  
  970: \ resolve                                              14oct92py
  971: 
  972:  : resolve-loop ( ghost resolve-list tcfa -- )
  973:     >r
  974:     BEGIN dup WHILE 
  975: \  	  dup >tag @ 2 = IF reswarn-forward THEN
  976: 	  resolve-warning 
  977: 	  r@ over >taddr @ 
  978: 	  2 pick >tag @
  979: 	  CASE	0 OF colon-resolve ENDOF
  980: 		1 OF addr-resolve ENDOF
  981: 		2 OF doer-resolve ENDOF
  982: 	  ENDCASE
  983: 	  @ \ next list element
  984:     REPEAT 2drop rdrop 
  985:   ;
  986: 
  987: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
  988: \  >r dup >link @
  989: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
  990: 
  991: \ exists                                                9may93jaw
  992: 
  993: Variable TWarnings
  994: TWarnings on
  995: Variable Exists-Warnings
  996: Exists-Warnings on
  997: 
  998: : exists ( ghost tcfa -- )
  999:   over GhostNames
 1000:   BEGIN @ dup
 1001:   WHILE 2dup cell+ @ =
 1002:   UNTIL
 1003:         2 cells + count
 1004:         TWarnings @ Exists-Warnings @ and
 1005:         IF warnhead type ."  exists"
 1006:         ELSE 2drop THEN
 1007:         drop swap >link !
 1008:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1009:   THEN ;
 1010: 
 1011: : resolve  ( ghost tcfa -- )
 1012: \G resolve referencies to ghost with tcfa
 1013:     \ is ghost resolved?, second resolve means another definition with the
 1014:     \ same name
 1015:     over undefined? 0= IF  exists EXIT THEN
 1016:     \ get linked-list
 1017:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1018:     \ mark ghost as resolved
 1019:     dup r@ >link ! <res> r@ >magic !
 1020:     \ loop through forward referencies
 1021:     r> -rot 
 1022:     comp-state @ >r Resolving comp-state !
 1023:     resolve-loop 
 1024:     r> comp-state !
 1025: 
 1026:     ['] noop IS resolve-warning 
 1027:   ;
 1028: 
 1029: \ gexecute ghost,                                      01nov92py
 1030: 
 1031: : is-forward   ( ghost -- )
 1032:   colonmark, 0 (refered) ; \ compile space for call
 1033: 
 1034: : is-resolved   ( ghost -- )
 1035:   >link @ colon, ; \ compile-call
 1036: 
 1037: : gexecute   ( ghost -- )
 1038:   dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;
 1039: 
 1040: : addr,  ( ghost -- )
 1041:   dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1042: 
 1043: \ !! : ghost,     ghost  gexecute ;
 1044: 
 1045: \ .unresolved                                          11may93jaw
 1046: 
 1047: variable ResolveFlag
 1048: 
 1049: \ ?touched                                             11may93jaw
 1050: 
 1051: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1052:                                0 <> and ;
 1053: 
 1054: : .forwarddefs ( ghost -- )
 1055: 	."  appeared in:"
 1056: 	>link
 1057: 	BEGIN	@ dup
 1058: 	WHILE	cr 5 spaces
 1059: 		dup >ghost @ >ghostname type
 1060: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1061: 		."  line " dup >line @ .dec
 1062: 	REPEAT 
 1063: 	drop ;
 1064: 
 1065: : ?resolved  ( ghostname -- )
 1066:   dup cell+ @ ?touched
 1067:   IF  	dup 
 1068: 	cell+ cell+ count cr type ResolveFlag on 
 1069: 	cell+ @ .forwarddefs
 1070:   ELSE 	drop 
 1071:   THEN ;
 1072: 
 1073: >MINIMAL
 1074: : .unresolved  ( -- )
 1075:   ResolveFlag off cr ." Unresolved: "
 1076:   Ghostnames
 1077:   BEGIN @ dup
 1078:   WHILE dup ?resolved
 1079:   REPEAT drop ResolveFlag @
 1080:   IF
 1081:       -1 abort" Unresolved words!"
 1082:   ELSE
 1083:       ." Nothing!"
 1084:   THEN
 1085:   cr ;
 1086: 
 1087: : .stats
 1088:   base @ >r decimal
 1089:   cr ." named Headers: " headers-named @ . 
 1090:   r> base ! ;
 1091: 
 1092: >CROSS
 1093: \ Header states                                        12dec92py
 1094: 
 1095: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
 1096: 
 1097: VARIABLE ^imm
 1098: 
 1099: >TARGET
 1100: : immediate     40 flag!
 1101:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1102:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1103:                 <imm> ^imm @ ! ;
 1104: : restrict      20 flag! ;
 1105: 
 1106: : isdoer	
 1107: \G define a forth word as doer, this makes obviously only sence on
 1108: \G forth processors such as the PSC1000
 1109: 		<do:> last-header-ghost @ >magic ! ;
 1110: >CROSS
 1111: 
 1112: \ ALIAS2 ansforth conform alias                          9may93jaw
 1113: 
 1114: : ALIAS2 create here 0 , DOES> @ execute ;
 1115: \ usage:
 1116: \ ' <name> alias2 bla !
 1117: 
 1118: \ Target Header Creation                               01nov92py
 1119: 
 1120: >TARGET
 1121: : string,  ( addr count -- )
 1122:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
 1123: : name,  ( "name" -- )  bl word count T string, cfalign H ;
 1124: : view,   ( -- ) ( dummy ) ;
 1125: >CROSS
 1126: 
 1127: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 1128: 
 1129: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 1130: \ contains the file-id of the documentation file
 1131: 
 1132: : T-\G ( -- )
 1133:     source >in @ /string doc-file-id write-line throw
 1134:     postpone \ ;
 1135: 
 1136: Variable to-doc  to-doc on
 1137: 
 1138: : cross-doc-entry  ( -- )
 1139:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 1140:     IF
 1141: 	s" " doc-file-id write-line throw
 1142: 	s" make-doc " doc-file-id write-file throw
 1143: 	tlast @ >image count $1F and doc-file-id write-file throw
 1144: 	>in @
 1145: 	[char] ( parse 2drop
 1146: 	[char] ) parse doc-file-id write-file throw
 1147: 	s"  )" doc-file-id write-file throw
 1148: 	[char] \ parse 2drop					
 1149: 	T-\G
 1150: 	>in !
 1151:     THEN ;
 1152: 
 1153: \ Target TAGS creation
 1154: 
 1155: s" kernel.TAGS" r/w create-file throw value tag-file-id
 1156: \ contains the file-id of the tags file
 1157: 
 1158: Create tag-beg 2 c,  7F c, bl c,
 1159: Create tag-end 2 c,  bl c, 01 c,
 1160: Create tag-bof 1 c,  0C c,
 1161: 
 1162: 2variable last-loadfilename 0 0 last-loadfilename 2!
 1163: 	    
 1164: : put-load-file-name ( -- )
 1165:     loadfilename 2@ last-loadfilename 2@ d<>
 1166:     IF
 1167: 	tag-bof count tag-file-id write-line throw
 1168: 	sourcefilename 2dup
 1169: 	tag-file-id write-file throw
 1170: 	last-loadfilename 2!
 1171: 	s" ,0" tag-file-id write-line throw
 1172:     THEN ;
 1173: 
 1174: : cross-tag-entry  ( -- )
 1175:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 1176:     IF
 1177: 	put-load-file-name
 1178: 	source >in @ min tag-file-id write-file throw
 1179: 	tag-beg count tag-file-id write-file throw
 1180: 	tlast @ >image count $1F and tag-file-id write-file throw
 1181: 	tag-end count tag-file-id write-file throw
 1182: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 1183: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 1184: 	s" ,0" tag-file-id write-line throw
 1185: 	base !
 1186:     THEN ;
 1187: 
 1188: \ Check for words
 1189: 
 1190: Defer skip? ' false IS skip?
 1191: 
 1192: : skipdef ( <name> -- )
 1193: \G skip definition of an undefined word in undef-words mode
 1194:     ghost dup forward?
 1195:     IF  >magic <skip> swap !
 1196:     ELSE drop THEN ;
 1197: 
 1198: : defined? ( -- flag ) \ name
 1199:     ghost undefined? 0= ;
 1200: 
 1201: : defined2? ( -- flag ) \ name
 1202: \G return true for anything else than forward, even for <skip>
 1203: \G that's what we want
 1204:     ghost forward? 0= ;
 1205: 
 1206: : needed? ( -- flag ) \ name
 1207: \G returns a false flag when
 1208: \G a word is not defined
 1209: \G a forward reference exists
 1210: \G so the definition is not skipped!
 1211:     bl word gfind
 1212:     IF dup undefined?
 1213: 	nip
 1214: 	0=
 1215:     ELSE  drop true  THEN ;
 1216: 
 1217: : doer? ( -- flag ) \ name
 1218:     ghost >magic @ <do:> = ;
 1219: 
 1220: : skip-defs ( -- )
 1221:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 1222: 
 1223: \ Target header creation
 1224: 
 1225: Variable CreateFlag
 1226: CreateFlag off
 1227: 
 1228: Variable NoHeaderFlag
 1229: NoHeaderFlag off
 1230: 
 1231: : 0.r ( n1 n2 -- ) 
 1232:     base @ >r hex 
 1233:     0 swap <# 0 ?DO # LOOP #> type 
 1234:     r> base ! ;
 1235: : .sym
 1236:   bounds 
 1237:   DO I c@ dup
 1238: 	CASE	'/ OF drop ." \/" ENDOF
 1239: 		'\ OF drop ." \\" ENDOF
 1240: 		dup OF emit ENDOF
 1241: 	ENDCASE
 1242:     LOOP ;
 1243: 
 1244: : (Theader ( "name" -- ghost )
 1245:     \  >in @ bl word count type 2 spaces >in !
 1246:     \ wordheaders will always be compiled to rom
 1247:     switchrom
 1248:     \ build header in target
 1249:     NoHeaderFlag @
 1250:     IF  NoHeaderFlag off
 1251:     ELSE
 1252: 	T align H view,
 1253: 	tlast @ dup 0> IF  T 1 cells - H THEN T A, H  there tlast !
 1254: 	1 headers-named +!	\ Statistic
 1255: 	>in @ T name, H >in !
 1256:     THEN
 1257:     T cfalign here H tlastcfa !
 1258:     \ Symbol table
 1259: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 1260:     CreateFlag @
 1261:     IF	\ for a created word we need also a definition in target
 1262: 	\ to execute the created word while compile time
 1263: 	\ dont mind if a alias is defined twice
 1264: 	Warnings @ >r Warnings off
 1265: 	>in @ alias2 swap >in !         \ create alias in target
 1266: 	r> Warnings !
 1267: 	>in @ ghost swap >in !
 1268: 	swap also ghosts ' previous swap !     \ tick ghost and store in alias
 1269: 	CreateFlag off
 1270:     ELSE ghost
 1271:     THEN
 1272:     dup Last-Header-Ghost !
 1273:     dup >magic ^imm !     \ a pointer for immediate
 1274:     Already @
 1275:     IF  dup >end tdoes !
 1276:     ELSE 0 tdoes !
 1277:     THEN
 1278:     80 flag!
 1279:     cross-doc-entry cross-tag-entry ;
 1280: 
 1281: VARIABLE ;Resolve 1 cells allot
 1282: \ this is the resolver information from ":"
 1283: \ resolving is done by ";"
 1284: 
 1285: : Theader  ( "name" -- ghost )
 1286:   (THeader dup there resolve 0 ;Resolve ! ;
 1287: 
 1288: >TARGET
 1289: : Alias    ( cfa -- ) \ name
 1290:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1291:     dup 0< s" prims" T $has? H 0= and
 1292:     IF
 1293: 	.sourcepos ." needs prim: " >in @ bl word count type >in ! cr
 1294:     THEN
 1295:     (THeader over resolve T A, H 80 flag! ;
 1296: : Alias:   ( cfa -- ) \ name
 1297:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1298:     dup 0< s" prims" T $has? H 0= and
 1299:     IF
 1300: 	.sourcepos ." needs doer: " >in @ bl word count type >in ! cr
 1301:     THEN
 1302:     ghost tuck swap resolve <do:> swap >magic ! ;
 1303: 
 1304: Variable prim#
 1305: : first-primitive ( n -- )  prim# ! ;
 1306: : Primitive  ( -- ) \ name
 1307:     prim# @ T Alias H  -1 prim# +! ;
 1308: >CROSS
 1309: 
 1310: \ Conditionals and Comments                            11may93jaw
 1311: 
 1312: : ;Cond
 1313:   postpone ;
 1314:   swap ! ;  immediate
 1315: 
 1316: : Cond: ( -- ) \ name {code } ;
 1317:   atonce on
 1318:   ghost
 1319:   >exec
 1320:   :NONAME ;
 1321: 
 1322: : restrict? ( -- )
 1323: \ aborts on interprete state - ae
 1324:   state @ 0= ABORT" CROSS: Restricted" ;
 1325: 
 1326: : Comment ( -- )
 1327:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
 1328: 
 1329: Comment (       Comment \
 1330: 
 1331: \ compile                                              10may93jaw
 1332: 
 1333: : compile  ( -- ) \ name
 1334:   restrict?
 1335:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
 1336:   0> ( immediate? )
 1337:   IF    >exec @ compile,
 1338:   ELSE  postpone literal postpone gexecute  THEN ;
 1339:                                         immediate
 1340: 
 1341: : [G'] 
 1342: \G ticks a ghost and returns its address
 1343:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1344:   state @
 1345:   IF   postpone literal
 1346:   THEN ; immediate
 1347: 
 1348: : ghost>cfa
 1349:   dup undefined? ABORT" CROSS: forward " >link @ ;
 1350:                
 1351: >TARGET
 1352: 
 1353: : '  ( -- cfa ) 
 1354: \ returns the target-cfa of a ghost
 1355:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1356:   ghost>cfa ;
 1357: 
 1358: Cond: [']  T ' H alit, ;Cond
 1359: 
 1360: >CROSS
 1361: 
 1362: : [T']
 1363: \ returns the target-cfa of a ghost, or compiles it as literal
 1364:   postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
 1365: 
 1366: \ \ threading modell					13dec92py
 1367: \ modularized						14jun97jaw
 1368: 
 1369: : fillcfa   ( usedcells -- )
 1370:   T cells H xt>body swap - 0 ?DO 0 T c, tchar H +LOOP ;
 1371: 
 1372: : (>body)   ( cfa -- pfa ) xt>body + ;		' (>body) T IS >body H
 1373: 
 1374: : (doer,)   ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ;   ' (doer,) IS doer,
 1375: 
 1376: : (docol,)  ( -- ) [G'] :docol doer, ;		' (docol,) IS docol,
 1377: 
 1378: : (doprim,) ( -- )
 1379:   there xt>body + ca>native T a, H 1 fillcfa ;	' (doprim,) IS doprim,
 1380: 
 1381: : (doeshandler,) ( -- ) 
 1382:   T cfalign H compile :doesjump T 0 , H ; 	' (doeshandler,) IS doeshandler,
 1383: 
 1384: : (dodoes,) ( does-action-ghost -- )
 1385:   ]comp [G'] :dodoes gexecute comp[
 1386:   addr,
 1387:   T here H tcell - reloff 2 fillcfa ;		' (dodoes,) IS dodoes,
 1388: 
 1389: : (lit,) ( n -- )   compile lit T  ,  H ;	' (lit,) IS lit,
 1390: 
 1391: \ if we dont produce relocatable code alit, defaults to lit, jaw
 1392: has? relocate
 1393: [IF]
 1394: : (alit,) ( n -- )  compile lit T  a, H ;	' (alit,) IS alit,
 1395: [ELSE]
 1396: : (alit,) ( n -- )  lit, ;			' (alit,) IS alit,
 1397: [THEN]
 1398: 
 1399: : (fini,)         compile ;s ;                ' (fini,) IS fini,
 1400: 
 1401: [IFUNDEF] (code) 
 1402: Defer (code)
 1403: Defer (end-code)
 1404: [THEN]
 1405: 
 1406: >TARGET
 1407: : Code
 1408:   defempty?
 1409:   (THeader there resolve
 1410:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
 1411:   doprim, 
 1412:   [THEN]
 1413:   depth (code) ;
 1414: 
 1415: : Code:
 1416:   defempty?
 1417:     ghost dup there ca>native resolve  <do:> swap >magic !
 1418:     depth (code) ;
 1419: 
 1420: : end-code
 1421:     (end-code)
 1422:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1423:     ELSE true ABORT" CROSS: Stack empty" THEN
 1424:     ;
 1425: 
 1426: ( Cond ) : chars tchar * ; ( Cond )
 1427: 
 1428: >CROSS
 1429: 
 1430: \ tLiteral                                             12dec92py
 1431: 
 1432: >TARGET
 1433: Cond: \G  T-\G ;Cond
 1434: 
 1435: Cond:  Literal ( n -- )   restrict? lit, ;Cond
 1436: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
 1437: 
 1438: : Char ( "<char>" -- )  bl word char+ c@ ;
 1439: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
 1440: 
 1441: \ some special literals					27jan97jaw
 1442: 
 1443: \ !! Known Bug: Special Literals and plug-ins work only correct
 1444: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
 1445: 
 1446: Cond: MAXU
 1447:   restrict? 
 1448:   tcell 1 cells u> 
 1449:   IF	compile lit tcell 0 ?DO FF T c, H LOOP 
 1450:   ELSE	$ffffffff lit, THEN
 1451:   ;Cond
 1452: 
 1453: Cond: MINI
 1454:   restrict?
 1455:   tcell 1 cells u>
 1456:   IF	compile lit bigendian 
 1457:   	IF	80 T c, H tcell 1 ?DO 0 T c, H LOOP 
 1458:   	ELSE  	tcell 1 ?DO 0 T c, H LOOP 80 T c, H
 1459:   	THEN
 1460:   ELSE	tcell 2 = IF $8000 ELSE $80000000 THEN lit, THEN
 1461:   ;Cond
 1462:  
 1463: Cond: MAXI
 1464:  restrict?
 1465:  tcell 1 cells u>
 1466:  IF	compile lit bigendian 
 1467: 	IF 	7F T c, H tcell 1 ?DO FF T c, H LOOP
 1468: 	ELSE 	tcell 1 ?DO FF T c, H LOOP 7F T c, H
 1469:  	THEN
 1470:  ELSE	tcell 2 = IF $7fff ELSE $7fffffff THEN lit, THEN
 1471:  ;Cond
 1472: 
 1473: >CROSS
 1474: \ Target compiling loop                                12dec92py
 1475: \ ">tib trick thrown out                               10may93jaw
 1476: \ number? defined at the top                           11may93jaw
 1477: 
 1478: \ compiled word might leave items on stack!
 1479: : tcom ( in name -- )
 1480:   gfind  ?dup  IF    0> IF    nip >exec @ execute
 1481:                         ELSE  nip gexecute  THEN EXIT THEN
 1482:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
 1483:                ELSE  2drop >in !
 1484:                ghost gexecute THEN  ;
 1485: 
 1486: >TARGET
 1487: \ : ; DOES>                                            13dec92py
 1488: \ ]                                                     9may93py/jaw
 1489: 
 1490: : ] state on
 1491:     Compiling comp-state !
 1492:     BEGIN
 1493:         BEGIN >in @ bl word
 1494:               dup c@ 0= WHILE 2drop refill 0=
 1495:               ABORT" CROSS: End of file while target compiling"
 1496:         REPEAT
 1497:         tcom
 1498:         state @
 1499:         0=
 1500:     UNTIL ;
 1501: 
 1502: \ by the way: defining a second interpreter (a compiler-)loop
 1503: \             is not allowed if a system should be ans conform
 1504: 
 1505: : : ( -- colon-sys ) \ Name
 1506:   defempty?
 1507:   constflag off \ don't let this flag work over colon defs
 1508: 		\ just to go sure nothing unwanted happens
 1509:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
 1510:   (THeader ;Resolve ! there ;Resolve cell+ !
 1511:   docol, ]comp depth T ] H ;
 1512: 
 1513: : :noname ( -- colon-sys )
 1514:   T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
 1515: 
 1516: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
 1517: 
 1518: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
 1519: 
 1520: >CROSS
 1521: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
 1522:          ;Resolve cell+ @ ;
 1523: 
 1524: >TARGET
 1525: 
 1526: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
 1527: 
 1528: Cond: ; ( -- ) restrict?
 1529:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1530:                           ELSE true ABORT" CROSS: Stack empty" THEN
 1531:                fini,
 1532:                comp[
 1533:                state off
 1534:                ;Resolve @
 1535:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
 1536: 		Interpreting comp-state !
 1537:                ;Cond
 1538: Cond: [  restrict? state off Interpreting comp-state ! ;Cond
 1539: 
 1540: >CROSS
 1541: 
 1542: Create GhostDummy ghostheader
 1543: <res> GhostDummy >magic !
 1544: 
 1545: : !does ( does-action -- )
 1546: \ !! zusammenziehen und dodoes, machen!
 1547:     tlastcfa @ [G'] :dovar killref
 1548: \    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
 1549: \ !! geht so nicht, da dodoes, ghost will!
 1550:     GhostDummy >link ! GhostDummy 
 1551:     tlastcfa @ >tempdp dodoes, tempdp> ;
 1552: 
 1553: >TARGET
 1554: Cond: DOES> restrict?
 1555:         compile (does>) doeshandler, 
 1556: 	\ resolve words made by builders
 1557: 	tdoes @ ?dup IF  @ T here H resolve THEN
 1558:         ;Cond
 1559: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
 1560: 
 1561: >CROSS
 1562: \ Creation                                             01nov92py
 1563: 
 1564: \ Builder                                               11may93jaw
 1565: 
 1566: : Builder    ( Create-xt do:-xt "name" -- )
 1567: \ builds up a builder in current vocabulary
 1568: \ create-xt is executed when word is interpreted
 1569: \ do:-xt is executet when the created word from builder is executed
 1570: \ for do:-xt an additional entry after the normal ghost-enrys is used
 1571: 
 1572:   >in @ alias2 swap dup >in ! >r >r
 1573:   Make-Ghost 
 1574:   rot swap >exec dup @ ['] NoExec <>
 1575:   IF 2drop ELSE ! THEN
 1576:   ,
 1577:   r> r> >in !
 1578:   also ghosts ' previous swap ! ;
 1579: \  DOES>  dup >exec @ execute ;
 1580: 
 1581: : gdoes,  ( ghost -- )
 1582: \ makes the codefield for a word that is built
 1583:   >end @ dup undefined? 0=
 1584:   IF
 1585: 	dup >magic @ <do:> =
 1586: 	IF 	 doer, 
 1587: 	ELSE	dodoes,
 1588: 	THEN
 1589: 	EXIT
 1590:   THEN
 1591: \  compile :dodoes gexecute
 1592: \  T here H tcell - reloff 
 1593:   2 refered 
 1594:   0 fillcfa
 1595:   ;
 1596: 
 1597: : TCreate ( <name> -- )
 1598:   executed-ghost @
 1599:   CreateFlag on
 1600:   create-forward-warn
 1601:   IF ['] reswarn-forward IS resolve-warning THEN
 1602:   Theader >r dup gdoes,
 1603: \ stores execution symantic in the built word
 1604:   >end @ >exec @ r> >exec ! ;
 1605: 
 1606: : RTCreate ( <name> -- )
 1607: \ creates a new word with code-field in ram
 1608:   executed-ghost @
 1609:   CreateFlag on
 1610:   create-forward-warn
 1611:   IF ['] reswarn-forward IS resolve-warning THEN
 1612:   \ make Alias
 1613:   (THeader there 0 T a, H 80 flag! ( S executed-ghost new-ghost )
 1614:   \ store  poiter to code-field
 1615:   switchram T cfalign H
 1616:   there swap T ! H
 1617:   there tlastcfa ! 
 1618:   dup there resolve 0 ;Resolve !
 1619:   >r dup gdoes,
 1620:   >end @ >exec @ r> >exec ! ;
 1621: 
 1622: : Build:  ( -- [xt] [colon-sys] )
 1623:   :noname postpone TCreate ;
 1624: 
 1625: : BuildSmart:  ( -- [xt] [colon-sys] )
 1626:   :noname
 1627:   [ T has? rom H [IF] ]
 1628:   postpone RTCreate
 1629:   [ [ELSE] ]
 1630:   postpone TCreate 
 1631:   [ [THEN] ] ;
 1632: 
 1633: : gdoes>  ( ghost -- addr flag )
 1634:   executed-ghost @
 1635:   state @ IF  gexecute true EXIT  THEN
 1636:   >link @ T >body H false ;
 1637: 
 1638: \ DO: ;DO                                               11may93jaw
 1639: \ changed to ?EXIT                                      10may93jaw
 1640: 
 1641: : DO:     ( -- addr [xt] [colon-sys] )
 1642:   here ghostheader
 1643:   :noname postpone gdoes> postpone ?EXIT ;
 1644: 
 1645: : by:     ( -- addr [xt] [colon-sys] ) \ name
 1646:   ghost
 1647:   :noname postpone gdoes> postpone ?EXIT ;
 1648: 
 1649: : ;DO ( addr [xt] [colon-sys] -- addr )
 1650:   postpone ;    ( S addr xt )
 1651:   over >exec ! ; immediate
 1652: 
 1653: : by      ( -- addr ) \ Name
 1654:   ghost >end @ ;
 1655: 
 1656: >TARGET
 1657: \ Variables and Constants                              05dec92py
 1658: 
 1659: Build:  ( n -- ) ;
 1660: by: :docon ( ghost -- n ) T @ H ;DO
 1661: Builder (Constant)
 1662: 
 1663: Build:  ( n -- ) T , H ;
 1664: by (Constant)
 1665: Builder Constant
 1666: 
 1667: Build:  ( n -- ) T A, H ;
 1668: by (Constant)
 1669: Builder AConstant
 1670: 
 1671: Build:  ( d -- ) T , , H ;
 1672: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
 1673: Builder 2Constant
 1674: 
 1675: BuildSmart: ;
 1676: by: :dovar ( ghost -- addr ) ;DO
 1677: Builder Create
 1678: 
 1679: T has? rom H [IF]
 1680: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 1681: by (Constant)
 1682: Builder Variable
 1683: [ELSE]
 1684: Build: T 0 , H ;
 1685: by Create
 1686: Builder Variable
 1687: [THEN]
 1688: 
 1689: T has? rom H [IF]
 1690: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
 1691: by (Constant)
 1692: Builder 2Variable
 1693: [ELSE]
 1694: Build: T 0 , 0 , H ;
 1695: by Create
 1696: Builder 2Variable
 1697: [THEN]
 1698: 
 1699: T has? rom H [IF]
 1700: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 1701: by (Constant)
 1702: Builder AVariable
 1703: [ELSE]
 1704: Build: T 0 A, H ;
 1705: by Create
 1706: Builder AVariable
 1707: [THEN]
 1708: 
 1709: \ User variables                                       04may94py
 1710: 
 1711: >CROSS
 1712: Variable tup  0 tup !
 1713: Variable tudp 0 tudp !
 1714: : u,  ( n -- udp )
 1715:   tup @ tudp @ + T  ! H
 1716:   tudp @ dup T cell+ H tudp ! ;
 1717: : au, ( n -- udp )
 1718:   tup @ tudp @ + T A! H
 1719:   tudp @ dup T cell+ H tudp ! ;
 1720: >TARGET
 1721: 
 1722: Build: T 0 u, , H ;
 1723: by: :douser ( ghost -- up-addr )  T @ H tup @ + ;DO
 1724: Builder User
 1725: 
 1726: Build: T 0 u, , 0 u, drop H ;
 1727: by User
 1728: Builder 2User
 1729: 
 1730: Build: T 0 au, , H ;
 1731: by User
 1732: Builder AUser
 1733: 
 1734: BuildSmart: T , H ;
 1735: by (Constant)
 1736: Builder Value
 1737: 
 1738: BuildSmart: T A, H ;
 1739: by (Constant)
 1740: Builder AValue
 1741: 
 1742: BuildSmart:  ( -- ) [T'] noop T A, H ;
 1743: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 1744: Builder Defer
 1745: 
 1746: BuildSmart:  ( inter comp -- ) swap T immediate A, A, H ;
 1747: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 1748: Builder interpret/compile:
 1749: 
 1750: \ Sturctures                                           23feb95py
 1751: 
 1752: >CROSS
 1753: : nalign ( addr1 n -- addr2 )
 1754: \ addr2 is the aligned version of addr1 wrt the alignment size n
 1755:  1- tuck +  swap invert and ;
 1756: >TARGET
 1757: 
 1758: Build: ;
 1759: by: :dofield T @ H + ;DO
 1760: Builder (Field)
 1761: 
 1762: Build: ( align1 offset1 align size "name" --  align2 offset2 )
 1763:     rot dup T , H ( align1 align size offset1 )
 1764:     + >r nalign r> ;
 1765: by (Field)
 1766: Builder Field
 1767: 
 1768: : struct  T 1 chars 0 H ;
 1769: : end-struct  T 2Constant H ;
 1770: 
 1771: : cell% ( n -- size align )
 1772:     T 1 cells H dup ;
 1773: 
 1774: \ ' 2Constant Alias2 end-struct
 1775: \ 0 1 T Chars H 2Constant struct
 1776: 
 1777: \ structural conditionals                              17dec92py
 1778: 
 1779: >CROSS
 1780: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 1781: : sys?        ( sys -- sys )    dup 0= ?struc ;
 1782: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 1783: 
 1784: : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw
 1785: 
 1786: : >resolve    ( sys -- )        T here ( dup ." >" hex. ) over branchoffset swap ! H ;
 1787: 
 1788: : <resolve    ( sys -- )        T here ( dup ." <" hex. ) branchoffset , H ;
 1789: 
 1790: :noname compile branch T here branchoffset , H ;
 1791:   IS branch, ( target-addr -- )
 1792: :noname compile ?branch T here branchoffset , H ;
 1793:   IS ?branch, ( target-addr -- )
 1794: :noname compile branch T here 0 , H ;
 1795:   IS branchmark, ( -- branchtoken )
 1796: :noname compile ?branch T here 0 , H ;
 1797:   IS ?branchmark, ( -- branchtoken )
 1798: :noname T here 0 , H ;
 1799:   IS ?domark, ( -- branchtoken )
 1800: :noname dup T @ H ?struc T here over branchoffset swap ! H ;
 1801:   IS branchtoresolve, ( branchtoken -- )
 1802: :noname branchto, T here H ;
 1803:   IS branchtomark, ( -- target-addr )
 1804: 
 1805: >TARGET
 1806: 
 1807: \ Structural Conditionals                              12dec92py
 1808: 
 1809: Cond: BUT       restrict? sys? swap ;Cond
 1810: Cond: YET       restrict? sys? dup ;Cond
 1811: 
 1812: 0 [IF]
 1813: >CROSS
 1814: Variable tleavings
 1815: >TARGET
 1816: 
 1817: Cond: DONE   ( addr -- )  restrict? tleavings @
 1818:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
 1819:       tleavings ! drop ;Cond
 1820: 
 1821: >CROSS
 1822: : (leave)  T here H tleavings @ T , H  tleavings ! ;
 1823: >TARGET
 1824: 
 1825: Cond: LEAVE     restrict? compile branch (leave) ;Cond
 1826: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave)  ;Cond
 1827: 
 1828: [ELSE]
 1829:     \ !! This is WIP
 1830:     \ The problem is (?DO)!
 1831:     \ perhaps we need a plug-in for (?DO)
 1832:     
 1833: >CROSS
 1834: Variable tleavings 0 tleavings !
 1835: : (done) ( addr -- )
 1836:     tleavings @
 1837:     BEGIN  dup
 1838:     WHILE
 1839: 	>r dup r@ cell+ @ \ address of branch
 1840: 	u> 0=	   \ lower than DO?	
 1841:     WHILE
 1842: 	r@ 2 cells + @ \ branch token
 1843: 	branchtoresolve,
 1844: 	r@ @ r> free throw
 1845:     REPEAT  r>  THEN
 1846:     tleavings ! drop ;
 1847: 
 1848: >TARGET
 1849: 
 1850: Cond: DONE   ( addr -- )  restrict? (done) ;Cond
 1851: 
 1852: >CROSS
 1853: : (leave) ( branchtoken -- )
 1854:     3 cells allocate throw >r
 1855:     T here H r@ cell+ !
 1856:     r@ 2 cells + !
 1857:     tleavings @ r@ !
 1858:     r> tleavings ! ;
 1859: >TARGET
 1860: 
 1861: Cond: LEAVE     restrict? branchmark, (leave) ;Cond
 1862: Cond: ?LEAVE    restrict? compile 0=  ?branchmark, (leave)  ;Cond
 1863: 
 1864: [THEN]
 1865: 
 1866: >CROSS
 1867: \ !!JW ToDo : Move to general tools section
 1868: 
 1869: : to1 ( x1 x2 xn n -- addr )
 1870: \G packs n stack elements in a allocated memory region
 1871:    dup dup 1+ cells allocate throw dup >r swap 1+
 1872:    0 DO tuck ! cell+ LOOP
 1873:    drop r> ;
 1874: : 1to ( addr -- x1 x2 xn )
 1875: \G unpacks the elements saved by to1
 1876:     dup @ swap over cells + swap
 1877:     0 DO  dup @ swap 1 cells -  LOOP
 1878:     free throw ;
 1879: 
 1880: : loop]     branchto, dup <resolve tcell - (done) ;
 1881: 
 1882: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 1883: 
 1884: >TARGET
 1885: 
 1886: \ Structural Conditionals                              12dec92py
 1887: 
 1888: >TARGET
 1889: Cond: AHEAD     restrict? branchmark, ;Cond
 1890: Cond: IF        restrict? ?branchmark, ;Cond
 1891: Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
 1892: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
 1893: 
 1894: Cond: BEGIN     restrict? branchtomark, ;Cond
 1895: Cond: WHILE     restrict? sys? compile IF swap ;Cond
 1896: Cond: AGAIN     restrict? sys? branch, ;Cond
 1897: Cond: UNTIL     restrict? sys? ?branch, ;Cond
 1898: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
 1899: 
 1900: Cond: CASE      restrict? 0 ;Cond
 1901: Cond: OF        restrict? 1+ >r compile over compile =
 1902:                 compile IF compile drop r> ;Cond
 1903: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
 1904: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
 1905: 
 1906: \ Structural Conditionals                              12dec92py
 1907: 
 1908: :noname \ ?? i think 0 is too much! jaw
 1909:     0 compile (do)
 1910:     branchtomark,  2 to1 ;
 1911:   IS do, ( -- target-addr )
 1912: 
 1913: \ :noname
 1914: \     compile 2dup compile = compile IF
 1915: \     compile 2drop compile ELSE
 1916: \     compile (do) branchtomark, 2 to1 ;
 1917: \   IS ?do,
 1918:     
 1919: :noname
 1920:     0 compile (?do)  ?domark, (leave)
 1921:     branchtomark,  2 to1 ;
 1922:   IS ?do, ( -- target-addr )
 1923: :noname compile (for) branchtomark, ;
 1924:   IS for, ( -- target-addr )
 1925: :noname 1to compile (loop)  loop] compile unloop skiploop] ;
 1926:   IS loop, ( target-addr -- )
 1927: :noname 1to compile (+loop)  loop] compile unloop skiploop] ;
 1928:   IS +loop, ( target-addr -- )
 1929: :noname compile (next)  loop] compile unloop ;
 1930:   IS next, ( target-addr -- )
 1931: 
 1932: Cond: DO      	restrict? do, ;Cond
 1933: Cond: ?DO     	restrict? ?do, ;Cond
 1934: Cond: FOR	restrict? for, ;Cond
 1935: 
 1936: Cond: LOOP	restrict? sys? loop, ;Cond
 1937: Cond: +LOOP	restrict? sys? +loop, ;Cond
 1938: Cond: NEXT	restrict? sys? next, ;Cond
 1939: 
 1940: \ String words                                         23feb93py
 1941: 
 1942: : ,"            [char] " parse T string, align H ;
 1943: 
 1944: Cond: ."        restrict? compile (.")     T ," H ;Cond
 1945: Cond: S"        restrict? compile (S")     T ," H ;Cond
 1946: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
 1947: 
 1948: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 1949: : IS            T >address ' >body ! H ;
 1950: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 1951: : TO            T ' >body ! H ;
 1952: 
 1953: Cond: defers	T ' >body @ compile, H ;Cond
 1954: : on		T -1 swap ! H ; 
 1955: : off   	T 0 swap ! H ;
 1956: 
 1957: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 1958: 
 1959: \ linked list primitive
 1960: : linked        T here over @ A, swap ! H ;
 1961: : chained	T linked A, H ;
 1962: 
 1963: : err"   s" ErrLink linked" evaluate T , H
 1964:          [char] " parse T string, align H ;
 1965: 
 1966: : env"  [char] " parse s" EnvLink linked" evaluate
 1967:         T string, align , H ;
 1968: 
 1969: : 2env" [char] " parse s" EnvLink linked" evaluate
 1970:         here >r T string, align , , H
 1971:         r> dup T c@ H 80 and swap T c! H ;
 1972: 
 1973: \ compile must be last                                 22feb93py
 1974: 
 1975: Cond: compile ( -- ) restrict? \ name
 1976:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 1977:       0> IF    gexecute
 1978:          ELSE  dup >magic @ <imm> =
 1979:                IF   gexecute
 1980:                ELSE compile (compile) addr, THEN THEN ;Cond
 1981: 
 1982: Cond: postpone ( -- ) restrict? \ name
 1983:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 1984:       0> IF    gexecute
 1985:          ELSE  dup >magic @ <imm> =
 1986:                IF   gexecute
 1987: 	       ELSE compile (compile) addr, THEN THEN ;Cond
 1988: 	   
 1989: \ \ minimal definitions
 1990: 	   
 1991: >MINIMAL
 1992: also minimal
 1993: \ Usefull words                                        13feb93py
 1994: 
 1995: : KB  400 * ;
 1996: 
 1997: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 1998: 
 1999: \ it is useful to define our own structures and not to rely
 2000: \ on the words in the compiler
 2001: \ The words in the compiler might be defined with vocabularies
 2002: \ this doesn't work with our self-made compile-loop
 2003: 
 2004: Create parsed 20 chars allot	\ store word we parsed
 2005: 
 2006: : upcase
 2007:     parsed count bounds
 2008:     ?DO I c@ toupper I c! LOOP ;
 2009: 
 2010: : [ELSE]
 2011:     1 BEGIN
 2012: 	BEGIN bl word count dup WHILE
 2013: 	    comment? parsed place upcase parsed count
 2014: 	    2dup s" [IF]" compare 0= >r 
 2015: 	    2dup s" [IFUNDEF]" compare 0= >r
 2016: 	    2dup s" [IFDEF]" compare 0= r> or r> or
 2017: 	    IF   2drop 1+
 2018: 	    ELSE 2dup s" [ELSE]" compare 0=
 2019: 		IF   2drop 1- dup
 2020: 		    IF 1+
 2021: 		    THEN
 2022: 		ELSE
 2023: 		    2dup s" [ENDIF]" compare 0= >r
 2024: 		    s" [THEN]" compare 0= r> or
 2025: 		    IF 1- THEN
 2026: 		THEN
 2027: 	    THEN
 2028: 	    ?dup 0= ?EXIT
 2029: 	REPEAT
 2030: 	2drop refill 0=
 2031:     UNTIL drop ; immediate
 2032:   
 2033: : [THEN] ( -- ) ; immediate
 2034: 
 2035: : [ENDIF] ( -- ) ; immediate
 2036: 
 2037: : [IF] ( flag -- )
 2038:     0= IF postpone [ELSE] THEN ; immediate 
 2039: 
 2040: Cond: [IF]      postpone [IF] ;Cond
 2041: Cond: [THEN]    postpone [THEN] ;Cond
 2042: Cond: [ELSE]    postpone [ELSE] ;Cond
 2043: 
 2044: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 2045: 
 2046: : defined? defined? ;
 2047: : needed? needed? ;
 2048: : doer? doer? ;
 2049: 
 2050: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 2051: 
 2052: : directive? 
 2053:   bl word count [ ' target >wordlist ] aliteral search-wordlist 
 2054:   dup IF nip THEN ;
 2055: 
 2056: : [IFDEF]  >in @ directive? swap >in !
 2057: 	   0= IF defined? ELSE name 2drop true THEN
 2058: 	   postpone [IF] ;
 2059: 
 2060: : [IFUNDEF] defined? 0= postpone [IF] ;
 2061: 
 2062: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 2063: 
 2064: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 2065: 
 2066: \ C: \- \+ Conditional Compiling                         09jun93jaw
 2067: 
 2068: : C: >in @ defined? 0=
 2069:      IF    >in ! T : H
 2070:      ELSE drop
 2071:         BEGIN bl word dup c@
 2072:               IF   count comment? s" ;" compare 0= ?EXIT
 2073:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 2074:               THEN
 2075:         AGAIN
 2076:      THEN ;
 2077: 
 2078: also minimal
 2079: 
 2080: : d? d? ;
 2081: 
 2082: \G doesn't skip line when debug switch is on
 2083: : \D D? 0= IF postpone \ THEN ;
 2084: 
 2085: \G interprets the line if word is not defined
 2086: : \- defined? IF postpone \ THEN ;
 2087: 
 2088: \G interprets the line if word is defined
 2089: : \+ defined? 0= IF postpone \ THEN ;
 2090: 
 2091: Cond: \- \- ;Cond
 2092: Cond: \+ \+ ;Cond
 2093: Cond: \D \D ;Cond
 2094: 
 2095: : ?? bl word find IF execute ELSE drop 0 THEN ;
 2096: 
 2097: : needed:
 2098: \G defines ghost for words that we want to be compiled
 2099:   BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
 2100: 
 2101: previous
 2102: 
 2103: \ save-cross                                           17mar93py
 2104: 
 2105: >CROSS
 2106: Create magic  s" Gforth2x" here over allot swap move
 2107: 
 2108: bigendian 1+ \ strangely, in magic big=0, little=1
 2109: tcell 1 = 0 and or
 2110: tcell 2 = 2 and or
 2111: tcell 4 = 4 and or
 2112: tcell 8 = 6 and or
 2113: tchar 1 = $00 and or
 2114: tchar 2 = $28 and or
 2115: tchar 4 = $50 and or
 2116: tchar 8 = $78 and or
 2117: magic 7 + c!
 2118: 
 2119: : save-cross ( "image-name" "binary-name" -- )
 2120:   bl parse ." Saving to " 2dup type cr
 2121:   w/o bin create-file throw >r
 2122:   TNIL IF
 2123:       s" #! "   r@ write-file throw
 2124:       bl parse  r@ write-file throw
 2125:       s"  -i"   r@ write-file throw
 2126:       #lf       r@ emit-file throw
 2127:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 2128:       ?do
 2129: 	  bl over emit-file throw
 2130:       loop
 2131:       drop
 2132:       magic 8       r@ write-file throw \ write magic
 2133:   ELSE
 2134:       bl parse 2drop
 2135:   THEN
 2136:   image @ there 
 2137:   r@ write-file throw \ write image
 2138:   TNIL IF
 2139:       bit$  @ there 1- tcell>bit rshift 1+
 2140:                 r@ write-file throw \ write tags
 2141:   THEN
 2142:   r> close-file throw ;
 2143: 
 2144: : save-region ( addr len -- )
 2145:   bl parse w/o bin create-file throw >r
 2146:   swap >image swap r@ write-file throw
 2147:   r> close-file throw ;
 2148: 
 2149: \ words that should be in minimal
 2150: 
 2151: create s-buffer 50 chars allot
 2152: 
 2153: >MINIMAL
 2154: also minimal
 2155: 
 2156: bigendian Constant bigendian
 2157: : here there ;
 2158: : equ constant ;
 2159: : mark there constant ;
 2160: 
 2161: \ compiler directives
 2162: : >ram >ram ;
 2163: : >rom >rom ;
 2164: : >auto >auto ;
 2165: : >tempdp >tempdp ;
 2166: : tempdp> tempdp> ;
 2167: : const constflag on ;
 2168: : warnings name 3 = 0= twarnings ! drop ;
 2169: : | ;
 2170: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 2171: 
 2172: : save-cross save-cross ;
 2173: : save-region save-region ;
 2174: : tdump swap >image swap dump ;
 2175: 
 2176: also forth 
 2177: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 2178: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 2179: [IFDEF] builttag	: builttag builttag ;	[THEN]
 2180: previous
 2181: 
 2182: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 2183: : + + ;
 2184: : 1+ 1 + ;
 2185: : 2+ 2 + ;
 2186: : or or ;
 2187: : 1- 1- ;
 2188: : - - ;
 2189: : and and ;
 2190: : or or ;
 2191: : 2* 2* ;
 2192: : * * ;
 2193: : / / ;
 2194: : dup dup ;
 2195: : over over ;
 2196: : swap swap ;
 2197: : rot rot ;
 2198: : drop drop ;
 2199: : =   = ;
 2200: : 0=   0= ;
 2201: : lshift lshift ;
 2202: : 2/ 2/ ;
 2203: : . . ;
 2204: 
 2205: : all-words    ['] false    IS skip? ;
 2206: : needed-words ['] needed?  IS skip? ;
 2207: : undef-words  ['] defined2? IS skip? ;
 2208: : skipdef skipdef ;
 2209: 
 2210: : \  postpone \ ;  immediate
 2211: : \G T-\G ; immediate
 2212: : (  postpone ( ;  immediate
 2213: : include bl word count included ;
 2214: : require require ;
 2215: : .( [char] ) parse type ;
 2216: : ." [char] " parse type ;
 2217: : cr cr ;
 2218: 
 2219: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
 2220: only forth also minimal definitions
 2221: 
 2222: \ cross-compiler words
 2223: 
 2224: : decimal       decimal ;
 2225: : hex           hex ;
 2226: 
 2227: : tudp          T tudp H ;
 2228: : tup           T tup H ;
 2229: 
 2230: : doc-off       false T to-doc H ! ;
 2231: : doc-on        true  T to-doc H ! ;
 2232: [IFDEF] dbg : dbg dbg ; [THEN]
 2233: 
 2234: minimal
 2235: 
 2236: \ for debugging...
 2237: : order         order ;
 2238: : hwords         words ;
 2239: : words 	also ghosts words previous ;
 2240: : .s            .s ;
 2241: : bye           bye ;
 2242: 
 2243: \ turnkey direction
 2244: : H forth ; immediate
 2245: : T minimal ; immediate
 2246: : G ghosts ; immediate
 2247: 
 2248: : turnkey  0 set-order also Target definitions
 2249:            also Minimal also ;
 2250: 
 2251: \ these ones are pefered:
 2252: 
 2253: : lock   turnkey ;
 2254: : unlock previous forth also cross ;
 2255: 
 2256: : [[ also unlock ;
 2257: : ]] previous previous ;
 2258: 
 2259: unlock definitions also minimal
 2260: : lock   lock ;
 2261: lock

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