File:  [gforth] / gforth / cross.fs
Revision 1.75: download - view: text, annotated - select for diffs
Mon May 10 13:57:37 1999 UTC (24 years, 11 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
New word skipdef, skips word definition in undef-words mode.

    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: 
  122: 
  123:   
  124: 
  125: previous >CROSS
  126: 
  127: : .dec
  128:   base @ decimal swap . base ! ;
  129: 
  130: : .sourcepos
  131:   cr sourcefilename type ." :"
  132:   sourceline# .dec ;
  133: 
  134: : warnhead
  135: \G display error-message head
  136: \G perhaps with linenumber and filename
  137:   .sourcepos ." Warning: " ;
  138: 
  139: : empty? depth IF .sourcepos ." Stack not empty!"  THEN ;
  140: 
  141: stack-warn [IF]
  142: : defempty? empty? ;
  143: [ELSE]
  144: : defempty? ; immediate
  145: [THEN]
  146: 
  147: \ debugging
  148: 
  149: 0 [IF]
  150: 
  151: This implements debugflags for the cross compiler and the compiled
  152: images. It works identical to the has-flags in the environment.
  153: The debugflags are defined in a vocabluary. If the word exists and
  154: its value is true, the flag is switched on.
  155: 
  156: [THEN]
  157: 
  158: Vocabulary debugflags	\ debug flags for cross
  159: also debugflags get-order over
  160: Constant debugflags-wl
  161: set-order previous
  162: 
  163: : DebugFlag
  164:   get-current >r debugflags-wl set-current
  165:   SetValue
  166:   r> set-current ;
  167: 
  168: : Debug? ( adr u -- flag )
  169: \G return true if debug flag is defined or switched on
  170:   debugflags-wl search-wordlist
  171:   IF EXECUTE
  172:   ELSE false THEN ;
  173: 
  174: : D? ( <name> -- flag )
  175: \G return true if debug flag is defined or switched on
  176: \G while compiling we do not return the current value but
  177:   bl word count debug? ;
  178: 
  179: : [d?]
  180: \G compile the value-xt so the debug flag can be switched
  181: \G the flag must exist!
  182:   bl word count debugflags-wl search-wordlist
  183:   IF 	compile,
  184:   ELSE  -1 ABORT" unknown debug flag"
  185: 	\ POSTPONE false 
  186:   THEN ; immediate
  187: 
  188: \ \ GhostNames Ghosts                                  9may93jaw
  189: 
  190: \ second name source to search trough list
  191: 
  192: VARIABLE GhostNames
  193: 0 GhostNames !
  194: 
  195: : GhostName ( -- addr )
  196:     here GhostNames @ , GhostNames ! here 0 ,
  197:     bl word count
  198:     \ 2dup type space
  199:     string, \ !! cfalign ?
  200:     align ;
  201: 
  202: \ Ghost Builder                                        06oct92py
  203: 
  204: \ <T T> new version with temp variable                 10may93jaw
  205: 
  206: VARIABLE VocTemp
  207: 
  208: : <T  get-current VocTemp ! also Ghosts definitions ;
  209: : T>  previous VocTemp @ set-current ;
  210: 
  211: hex
  212: 4711 Constant <fwd>             4712 Constant <res>
  213: 4713 Constant <imm>             4714 Constant <do:>
  214: 4715 Constant <skip>
  215: 
  216: \ iForth makes only immediate directly after create
  217: \ make atonce trick! ?
  218: 
  219: Variable atonce atonce off
  220: 
  221: : NoExec true ABORT" CROSS: Don't execute ghost" ;
  222: 
  223: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  224: 
  225: : >magic ;		\ type of ghost
  226: : >link cell+ ;		\ pointer where ghost is in target, or if unresolved
  227: 			\ points to the where we have to resolve (linked-list)
  228: : >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
  229: : >end 3 cells + ;	\ room for additional tags
  230: 			\ for builder (create, variable...) words the
  231: 			\ execution symantics of words built are placed here
  232: 
  233: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
  234: Variable last-ghost	\ last ghost that is created
  235: Variable last-header-ghost \ last ghost definitions with header
  236: 
  237: : Make-Ghost ( "name" -- ghost )
  238:   >in @ GhostName swap >in !
  239:   <T Create atonce @ IF immediate atonce off THEN
  240:   here tuck swap ! ghostheader T>
  241:   dup last-ghost !
  242:   DOES> dup executed-ghost ! >exec @ execute ;
  243: 
  244: \ ghost words                                          14oct92py
  245: \                                          changed:    10may93py/jaw
  246: 
  247: : gfind   ( string -- ghost true/1 / string false )
  248: \ searches for string in word-list ghosts
  249:   dup count [ ' ghosts >wordlist ] ALiteral search-wordlist
  250:   dup IF >r >body nip r>  THEN ;
  251: 
  252: : gdiscover ( xt -- ghost true | xt false )
  253:   GhostNames
  254:   BEGIN @ dup
  255:   WHILE 2dup
  256:         cell+ @ dup >magic @ <fwd> <>
  257:         >r >link @ = r> and
  258:         IF cell+ @ nip true EXIT THEN
  259:   REPEAT
  260:   drop false ;
  261: 
  262: VARIABLE Already
  263: 
  264: : ghost   ( "name" -- ghost )
  265:   Already off
  266:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
  267:   drop  >in !  Make-Ghost ;
  268: 
  269: : >ghostname ( ghost -- adr len )
  270:   GhostNames
  271:   BEGIN @ dup
  272:   WHILE 2dup cell+ @ =
  273:   UNTIL nip 2 cells + count
  274:   ELSE  2drop 
  275: 	\ true abort" CROSS: Ghostnames inconsistent"
  276: 	s" ?!?!?!"
  277:   THEN ;
  278: 
  279: ' >ghostname ALIAS @name
  280: 
  281: : forward? ( ghost -- flag )
  282:   >magic @ <fwd> = ;
  283: 
  284: : undefined? ( ghost -- flag )
  285:   >magic @ dup <fwd> = swap <skip> = or ;
  286: 
  287: \ Predefined ghosts                                    12dec92py
  288: 
  289: ghost 0=                                        drop
  290: ghost branch    ghost ?branch                   2drop
  291: ghost (do)      ghost (?do)                     2drop
  292: ghost (for)                                     drop
  293: ghost (loop)    ghost (+loop)                   2drop
  294: ghost (next)                                    drop
  295: ghost unloop    ghost ;S                        2drop
  296: ghost lit       ghost (compile) ghost !         2drop drop
  297: ghost (does>)   ghost noop                      2drop
  298: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  299: ghost '                                         drop
  300: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  301: ghost :dovar					drop
  302: ghost over      ghost =         ghost drop      2drop drop
  303: ghost - drop
  304: ghost 2drop drop
  305: ghost 2dup drop
  306: 
  307: \ \ Parameter for target systems                         06oct92py
  308: 
  309: \ we define it ans like...
  310: wordlist Constant target-environment
  311: 
  312: VARIABLE env-current \ save information of current dictionary to restore with environ>
  313: 
  314: : >ENVIRON get-current env-current ! target-environment set-current ;
  315: : ENVIRON> env-current @ set-current ; 
  316: 
  317: >TARGET
  318: 
  319: : environment? ( adr len -- [ x ] true | false )
  320:   target-environment search-wordlist 
  321:   IF execute true ELSE false THEN ;
  322: 
  323: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
  324: 
  325: : has? 	bl word count T environment? H 
  326: 	IF 	\ environment variable is present, return its value
  327: 	ELSE	\ environment variable is not present, return false
  328: 		false \ debug true ABORT" arg" 
  329: 	THEN ;
  330: 
  331: : $has? T environment? H IF ELSE false THEN ;
  332: 
  333: >ENVIRON get-order get-current swap 1+ set-order
  334: true SetValue compiler
  335: true  SetValue cross
  336: true SetValue standard-threading
  337: >TARGET previous
  338: 
  339: 
  340: mach-file count included hex
  341: 
  342: >ENVIRON
  343: 
  344: T has? ec H
  345: [IF]
  346: false DefaultValue relocate
  347: false DefaultValue file
  348: false DefaultValue OS
  349: false DefaultValue prims
  350: false DefaultValue floating
  351: false DefaultValue glocals
  352: false DefaultValue dcomps
  353: false DefaultValue hash
  354: false DefaultValue xconds
  355: false DefaultValue header
  356: [THEN]
  357: 
  358: true DefaultValue interpreter
  359: true DefaultValue ITC
  360: false DefaultValue rom
  361: true DefaultValue standardthreading
  362: 
  363: >TARGET
  364: s" relocate" T environment? H 
  365: [IF]	SetValue NIL
  366: [ELSE]	>ENVIRON T NIL H SetValue relocate
  367: [THEN]
  368: 
  369: >CROSS
  370: 
  371: \ \ Create additional parameters                         19jan95py
  372: 
  373: \ currently cross only works for host machines with address-unit-bits
  374: \ eual to 8 because of s! and sc!
  375: \ but I start to query the environment just to modularize a little bit
  376: 
  377: : check-address-unit-bits ( -- )	
  378: \	s" ADDRESS-UNIT-BITS" environment?
  379: \	IF 8 <> ELSE true THEN
  380: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
  381: 
  382: \	shit, this doesn't work because environment? is only defined for 
  383: \	gforth.fi and not kernl???.fi
  384: 	;
  385: 
  386: check-address-unit-bits
  387: 8 Constant bits/byte	\ we define: byte is address-unit
  388: 
  389: 1 bits/byte lshift Constant maxbyte 
  390: \ this sets byte size for the target machine, an (probably right guess) jaw
  391: 
  392: T
  393: NIL		   	Constant TNIL
  394: cell               	Constant tcell
  395: cell<<             	Constant tcell<<
  396: cell>bit           	Constant tcell>bit
  397: bits/char          	Constant tbits/char
  398: bits/char H bits/byte T /      
  399: 			Constant tchar
  400: float             	Constant tfloat
  401: 1 bits/char lshift 	Constant tmaxchar
  402: [IFUNDEF] bits/byte
  403: 8			Constant tbits/byte
  404: [ELSE]
  405: bits/byte		Constant tbits/byte
  406: [THEN]
  407: H
  408: tbits/byte bits/byte /	Constant tbyte
  409: 
  410: 
  411: \ Variables                                            06oct92py
  412: 
  413: Variable image
  414: Variable tlast    TNIL tlast !  \ Last name field
  415: Variable tlastcfa \ Last code field
  416: Variable tdoes    \ Resolve does> calls
  417: Variable bit$
  418: 
  419: \ statistics						10jun97jaw
  420: 
  421: Variable headers-named 0 headers-named !
  422: Variable user-vars 0 user-vars !
  423: 
  424: : target>bitmask-size ( u1 -- u2 )
  425:   1- tcell>bit rshift 1+ ;
  426: 
  427: : allocatetarget ( size --- adr )
  428:   dup allocate ABORT" CROSS: No memory for target"
  429:   swap over swap erase ;
  430: 
  431: \ \ memregion.fs
  432: 
  433: 
  434: Variable last-defined-region    \ pointer to last defined region
  435: Variable region-link            \ linked list with all regions
  436: Variable mirrored-link          \ linked list for mirrored regions
  437: 0 dup mirrored-link ! region-link !
  438: 
  439: 
  440: : >rname 6 cells + ;
  441: : >rbm   5 cells + ;
  442: : >rmem  4 cells + ;
  443: : >rlink 3 cells + ;
  444: : >rdp 2 cells + ;
  445: : >rlen cell+ ;
  446: : >rstart ;
  447: 
  448: 
  449: : region ( addr len -- )                \G create a new region
  450:   \ check whether predefined region exists 
  451:   save-input bl word find >r >r restore-input throw r> r> 0= 
  452:   IF	\ make region
  453: 	drop
  454: 	save-input create restore-input throw
  455: 	here last-defined-region !
  456: 	over ( startaddr ) , ( length ) , ( dp ) ,
  457: 	region-link linked 0 , 0 , bl word count string,
  458:   ELSE	\ store new parameters in region
  459:         bl word drop
  460: 	>body >r r@ last-defined-region !
  461: 	r@ >rlen ! dup r@ >rstart ! r> >rdp !
  462:   THEN ;
  463: 
  464: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  465:   dup >rstart @ swap >rlen @ over + ;
  466: 
  467: : extent  ( region -- startaddr len )   \G returns the really used area
  468:   dup >rstart @ swap >rdp @ over - ;
  469: 
  470: : area ( region -- startaddr totallen ) \G returns the total area
  471:   dup >rstart swap >rlen @ ;
  472: 
  473: : mirrored                              \G mark a region as mirrored
  474:   mirrored-link
  475:   align linked last-defined-region @ , ;
  476: 
  477: : .addr ( u -- )
  478: \G prints a 16 or 32 Bit nice hex value
  479:   base @ >r hex
  480:   tcell 2 u>
  481:   IF s>d <# # # # # '. hold # # # # #> type
  482:   ELSE s>d <# # # # # # #> type
  483:   THEN r> base ! ;
  484: 
  485: : .regions                      \G display region statistic
  486: 
  487:   \ we want to list the regions in the right order
  488:   \ so first collect all regions on stack
  489:   0 region-link @
  490:   BEGIN dup WHILE dup @ REPEAT drop
  491:   BEGIN dup
  492:   WHILE cr
  493:         0 >rlink - >r
  494:         r@ >rname count tuck type
  495:         12 swap - 0 max spaces space
  496:         ." Start: " r@ >rstart @ dup .addr space
  497:         ." End: " r@ >rlen @ + .addr space
  498:         ." DP: " r> >rdp @ .addr
  499:   REPEAT drop
  500:   s" rom" T $has? H 0= ?EXIT
  501:   cr ." Mirrored:"
  502:   mirrored-link @
  503:   BEGIN dup
  504:   WHILE space dup cell+ @ >rname count type @
  505:   REPEAT drop cr
  506:   ;
  507: 
  508: \ -------- predefined regions
  509: 
  510: 0 0 region address-space
  511: \ total memory addressed and used by the target system
  512: 
  513: 0 0 region dictionary
  514: \ rom area for the compiler
  515: 
  516: T has? rom H
  517: [IF]
  518: 0 0 region ram-dictionary mirrored
  519: \ ram area for the compiler
  520: [ELSE]
  521: ' dictionary ALIAS ram-dictionary
  522: [THEN]
  523: 
  524: 0 0 region return-stack
  525: 
  526: 0 0 region data-stack
  527: 
  528: 0 0 region tib-region
  529: 
  530: ' dictionary ALIAS rom-dictionary
  531: 
  532: 
  533: : setup-target ( -- )   \G initialize targets memory space
  534:   s" rom" T $has? H
  535:   IF  \ check for ram and rom...
  536:       \ address-space area nip 0<>
  537:       ram-dictionary area nip 0<>
  538:       rom-dictionary area nip 0<>
  539:       and 0=
  540:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
  541:   THEN
  542:   address-space area nip
  543:   IF
  544:       address-space area
  545:   ELSE
  546:       dictionary area
  547:   THEN
  548:   nip 0=
  549:   ABORT" CROSS: define at least address-space or dictionary!!"
  550: 
  551:   \ allocate target for each region
  552:   region-link
  553:   BEGIN @ dup
  554:   WHILE dup
  555:         0 >rlink - >r
  556:         r@ >rlen @
  557:         IF      \ allocate mem
  558:                 r@ >rlen @ dup
  559: 
  560:                 allocatetarget dup image !
  561:                 r@ >rmem !
  562: 
  563:                 target>bitmask-size allocatetarget
  564:                 dup bit$ !
  565:                 r> >rbm !
  566: 
  567:         ELSE    r> drop THEN
  568:    REPEAT drop ;
  569: 
  570: \ MakeKernal                                           		22feb99jaw
  571: 
  572: : makekernel ( targetsize -- targetsize )
  573:   dup dictionary >rlen ! setup-target ;
  574: 
  575: >MINIMAL
  576: : makekernel makekernel ;
  577: >CROSS
  578: 
  579: \ \ switched tdp for rom support				03jun97jaw
  580: 
  581: \ second value is here to store some maximal value for statistics
  582: \ tempdp is also embedded here but has nothing to do with rom support
  583: \ (needs switched dp)
  584: 
  585: variable tempdp	0 ,	\ temporary dp for resolving
  586: variable tempdp-save
  587: 
  588: 0 [IF]
  589: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
  590: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
  591: 
  592: \
  593: variable sramdp		\ start of ram-area for forth
  594: variable sromdp		\ start of rom-area for forth
  595: 
  596: [THEN]
  597: 
  598: 
  599: 0 value tdp
  600: variable fixed		\ flag: true: no automatic switching
  601: 			\	false: switching is done automatically
  602: 
  603: \ Switch-Policy:
  604: \
  605: \ a header is always compiled into rom
  606: \ after a created word (create and variable) compilation goes to ram
  607: \
  608: \ Be careful: If you want to make the data behind create into rom
  609: \ you have to put >rom before create!
  610: 
  611: variable constflag constflag off
  612: 
  613: : activate ( region -- )
  614: \G next code goes to this region
  615:   >rdp to tdp ;
  616: 
  617: : (switchram)
  618:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
  619:   ram-dictionary activate ;
  620: 
  621: : switchram
  622:   constflag @
  623:   IF constflag off ELSE (switchram) THEN ;
  624: 
  625: : switchrom
  626:   fixed @ ?EXIT rom-dictionary activate ;
  627: 
  628: : >tempdp ( addr -- ) 
  629:   tdp tempdp-save ! tempdp to tdp tdp ! ;
  630: : tempdp> ( -- )
  631:   tempdp-save @ to tdp ;
  632: 
  633: : >ram  fixed off (switchram) fixed on ;
  634: : >rom  fixed off switchrom fixed on ;
  635: : >auto fixed off switchrom ;
  636: 
  637: 
  638: 
  639: \ : romstart dup sromdp ! romdp ! ;
  640: \ : ramstart dup sramdp ! ramdp ! ;
  641: 
  642: \ default compilation goes to rom
  643: \ when romable support is off, only the rom switch is used (!!)
  644: >auto
  645: 
  646: : there  tdp @ ;
  647: 
  648: >TARGET
  649: 
  650: \ \ Target Memory Handling
  651: 
  652: \ Byte ordering and cell size                          06oct92py
  653: 
  654: : cell+         tcell + ;
  655: : cells         tcell<< lshift ;
  656: : chars         tchar * ;
  657: : char+		tchar + ;
  658: : floats	tfloat * ;
  659:     
  660: >CROSS
  661: : cell/         tcell<< rshift ;
  662: >TARGET
  663: 20 CONSTANT bl
  664: \ TNIL Constant NIL
  665: 
  666: >CROSS
  667: 
  668: bigendian
  669: [IF]
  670:    : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
  671:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  672:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds
  673:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  674:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
  675:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  676:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
  677:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  678: [ELSE]
  679:    : S!  ( n addr -- )  >r s>d r> tcell bounds
  680:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  681:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
  682:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  683:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
  684:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  685:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
  686:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  687: [THEN]
  688: 
  689: : taddr>region ( taddr -- region | 0 )
  690: \G finds for a target-address the correct region
  691: \G returns 0 if taddr is not in range of a target memory region
  692:   region-link
  693:   BEGIN @ dup
  694:   WHILE dup >r
  695:         0 >rlink - >r
  696:         r@ >rlen @
  697:         IF      dup r@ borders within
  698:                 IF r> r> drop nip EXIT THEN
  699:         THEN
  700:         r> drop
  701:         r>
  702:   REPEAT
  703:   2drop 0 ;
  704: 
  705: : (>regionimage) ( taddr -- 'taddr )
  706:   dup
  707:   \ find region we want to address
  708:   taddr>region dup 0= ABORT" Address out of range!"
  709:   >r
  710:   \ calculate offset in region
  711:   r@ >rstart @ -
  712:   \ add regions real address in our memory
  713:   r> >rmem @ + ;
  714: 
  715: \ Bit string manipulation                               06oct92py
  716: \                                                       9may93jaw
  717: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
  718: : bits ( n -- n ) chars Bittable + c@ ;
  719: 
  720: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
  721: : +bit ( addr n -- )  >bit over c@ or swap c! ;
  722: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
  723: 
  724: : (relon) ( taddr -- )  bit$ @ swap cell/ +bit ;
  725: : (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;
  726: 
  727: : (>image) ( taddr -- absaddr ) image @ + ;
  728: 
  729: DEFER >image
  730: DEFER relon
  731: DEFER reloff
  732: DEFER correcter
  733: 
  734: T has? relocate H
  735: [IF]
  736: ' (relon) IS relon
  737: ' (reloff) IS reloff
  738: ' (>image) IS >image
  739: [ELSE]
  740: ' drop IS relon
  741: ' drop IS reloff
  742: ' (>regionimage) IS >image
  743: [THEN]
  744: 
  745: \ Target memory access                                 06oct92py
  746: 
  747: : align+  ( taddr -- rest )
  748:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
  749: : cfalign+  ( taddr -- rest )
  750:     \ see kernel.fs:cfaligned
  751:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
  752: 
  753: >TARGET
  754: : aligned ( taddr -- ta-addr )  dup align+ + ;
  755: \ assumes cell alignment granularity (as GNU C)
  756: 
  757: : cfaligned ( taddr1 -- taddr2 )
  758:     \ see kernel.fs
  759:     dup cfalign+ + ;
  760: 
  761: : @  ( taddr -- w )     >image S@ ;
  762: : !  ( w taddr -- )     >image S! ;
  763: : c@ ( taddr -- char )  >image Sc@ ;
  764: : c! ( char taddr -- )  >image Sc! ;
  765: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
  766: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
  767: 
  768: \ Target compilation primitives                        06oct92py
  769: \ included A!                                          16may93jaw
  770: 
  771: : here  ( -- there )    there ;
  772: : allot ( n -- )        tdp +! ;
  773: : ,     ( w -- )        T here H tcell T allot  ! H T here drop H ;
  774: : c,    ( char -- )     T here    tchar allot c! H ;
  775: : align ( -- )          T here H align+ 0 ?DO  bl T c, tchar H +LOOP ;
  776: : cfalign ( -- )
  777:     T here H cfalign+ 0 ?DO  bl T c, tchar H +LOOP ;
  778: 
  779: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
  780: : A!                    swap >address swap dup relon T ! H ;
  781: : A,    ( w -- )        >address T here H relon T , H ;
  782: 
  783: >CROSS
  784: 
  785: : tcmove ( source dest len -- )
  786: \G cmove in target memory
  787:   tchar * bounds
  788:   ?DO  dup T c@ H I T c! H 1+
  789:   tchar +LOOP  drop ;
  790: 
  791: \ \ Load Assembler
  792: 
  793: >TARGET
  794: H also Forth definitions
  795: 
  796: : X 	also target bl word find
  797: 	IF	state @ IF compile,
  798: 		ELSE execute THEN
  799: 	ELSE	previous ABORT" Cross: access method not supported!"
  800: 	THEN 
  801: 	previous ; immediate
  802: 
  803: [IFDEF] asm-include asm-include [THEN] hex
  804: 
  805: previous
  806: >CROSS H
  807: 
  808: \ \ --------------------        Compiler Plug Ins               01aug97jaw
  809: 
  810: \  Compiler States
  811: 
  812: Variable comp-state
  813: 0 Constant interpreting
  814: 1 Constant compiling
  815: 2 Constant resolving
  816: 3 Constant assembling
  817: 
  818: Defer lit, ( n -- )
  819: Defer alit, ( n -- )
  820: 
  821: Defer branch, ( target-addr -- )	\ compiles a branch
  822: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
  823: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
  824: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
  825: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
  826: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
  827: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
  828: Defer branchfrom, ( -- )		\ ?!
  829: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
  830: 
  831: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
  832: Defer colonmark, ( -- addr )		\ marks a colon call
  833: Defer colon-resolve ( tcfa addr -- )
  834: 
  835: Defer addr-resolve ( target-addr addr -- )
  836: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
  837: 
  838: Defer do,	( -- do-token )
  839: Defer ?do,	( -- ?do-token )
  840: Defer for,	( -- for-token )
  841: Defer loop,	( do-token / ?do-token -- )
  842: Defer +loop,	( do-token / ?do-token -- )
  843: Defer next,	( for-token )
  844: 
  845: [IFUNDEF] ca>native
  846: defer ca>native	
  847: [THEN]
  848: 
  849: >TARGET
  850: DEFER >body             \ we need the system >body
  851: 			\ and the target >body
  852: >CROSS
  853: T 2 cells H VALUE xt>body
  854: DEFER doprim,	\ compiles start of a primitive
  855: DEFER docol,   	\ compiles start of a colon definition
  856: DEFER doer,		
  857: DEFER fini,      \ compiles end of definition ;s
  858: DEFER doeshandler,
  859: DEFER dodoes,
  860: 
  861: DEFER ]comp     \ starts compilation
  862: DEFER comp[     \ ends compilation
  863: 
  864: : (cc) T a, H ;					' (cc) IS colon,
  865: 
  866: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
  867: : (ar) T ! H ;					' (ar) IS addr-resolve
  868: : (dr)  ( ghost res-pnt target-addr addr )
  869: 	>tempdp drop over 
  870: 	dup >magic @ <do:> =
  871: 	IF 	doer,
  872: 	ELSE	dodoes,
  873: 	THEN 
  874: 	tempdp> ;				' (dr) IS doer-resolve
  875: 
  876: : (cm) ( -- addr )
  877:     T here align H
  878:     -1 colon, ;					' (cm) IS colonmark,
  879: 
  880: >TARGET
  881: : compile, colon, ;
  882: >CROSS
  883: 
  884: \ file loading
  885: 
  886: : >fl-id   1 cells + ;
  887: : >fl-name 2 cells + ;
  888: 
  889: Variable filelist 0 filelist !
  890: Create NoFile ," #load-file#"
  891: 0 Value	 filemem
  892: : loadfile  FileMem ?dup IF >fl-name ELSE NoFile THEN ;
  893: 
  894: 1 [IF] \ !! JAW WIP
  895: 
  896: : add-included-file ( adr len -- )
  897: 	dup char+ >fl-name allocate throw >r
  898: 	r@ >fl-name place
  899: 	filelist @ r@ !
  900: 	r> dup filelist ! to FileMem
  901: 	;
  902: 
  903: : included? ( c-addr u -- f )
  904:   	filelist
  905: 	BEGIN	@ dup
  906: 	WHILE	>r r@ 1 cells + count compare 0=
  907: 		IF rdrop 2drop true EXIT THEN
  908: 		r>
  909: 	REPEAT
  910: 	2drop drop false ;	
  911: 
  912: false DebugFlag showincludedfiles
  913: 
  914: : included 
  915: 	[d?] showincludedfiles
  916: 	IF	cr ." Including: " 2dup type ." ..." THEN
  917: 	FileMem >r
  918: 	2dup add-included-file included 
  919: 	r> to FileMem ;
  920: 
  921: : include bl word count included ;
  922: 
  923: : require bl word count included ;
  924: 
  925: [THEN]
  926: 
  927: \ resolve structure
  928: 
  929: : >next ;		\ link to next field
  930: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
  931: : >taddr cell+ cell+ ;	
  932: : >ghost 3 cells + ;
  933: : >file 4 cells + ;
  934: : >line 5 cells + ;
  935: 
  936: : (refered) ( ghost addr tag -- )
  937: \G creates a reference to ghost at address taddr
  938:     rot >r here r@ >link @ , r> >link ! 
  939:     ( taddr tag ) ,
  940:     ( taddr ) , 
  941:     last-header-ghost @ , 
  942:     loadfile , 
  943:     sourceline# , 
  944:   ;
  945: 
  946: : refered ( ghost tag -- )
  947: \G creates a resolve structure
  948:     T here aligned H swap (refered)
  949:   ;
  950: 
  951: : killref ( addr ghost -- )
  952: \G kills a forward reference to ghost at position addr
  953: \G this is used to eleminate a :dovar refence after making a DOES>
  954:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
  955:     swap >r >link
  956:     BEGIN dup @ dup  ( addr last this )
  957:     WHILE dup >taddr @ r@ =
  958:  	 IF   @ over !
  959: 	 ELSE nip THEN
  960:     REPEAT rdrop 2drop 
  961:   ;
  962: 
  963: Defer resolve-warning
  964: 
  965: : reswarn-test ( ghost res-struct -- ghost res-struct )
  966:   over cr ." Resolving " >ghostname type dup ."  in " >ghost @ >ghostname type ;
  967: 
  968: : reswarn-forward ( ghost res-struct -- ghost res-struct )
  969:   over warnhead >ghostname type dup ."  is referenced in " 
  970:   >ghost @ >ghostname type ;
  971: 
  972: \ ' reswarn-test IS resolve-warning
  973:  
  974: \ resolve                                              14oct92py
  975: 
  976:  : resolve-loop ( ghost resolve-list tcfa -- )
  977:     >r
  978:     BEGIN dup WHILE 
  979: \  	  dup >tag @ 2 = IF reswarn-forward THEN
  980: 	  resolve-warning 
  981: 	  r@ over >taddr @ 
  982: 	  2 pick >tag @
  983: 	  CASE	0 OF colon-resolve ENDOF
  984: 		1 OF addr-resolve ENDOF
  985: 		2 OF doer-resolve ENDOF
  986: 	  ENDCASE
  987: 	  @ \ next list element
  988:     REPEAT 2drop rdrop 
  989:   ;
  990: 
  991: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
  992: \  >r dup >link @
  993: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
  994: 
  995: \ exists                                                9may93jaw
  996: 
  997: Variable TWarnings
  998: TWarnings on
  999: Variable Exists-Warnings
 1000: Exists-Warnings on
 1001: 
 1002: : exists ( ghost tcfa -- )
 1003:   over GhostNames
 1004:   BEGIN @ dup
 1005:   WHILE 2dup cell+ @ =
 1006:   UNTIL
 1007:         2 cells + count
 1008:         TWarnings @ Exists-Warnings @ and
 1009:         IF warnhead type ."  exists"
 1010:         ELSE 2drop THEN
 1011:         drop swap >link !
 1012:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1013:   THEN ;
 1014: 
 1015: : resolve  ( ghost tcfa -- )
 1016: \G resolve referencies to ghost with tcfa
 1017:     \ is ghost resolved?, second resolve means another definition with the
 1018:     \ same name
 1019:     over undefined? 0= IF  exists EXIT THEN
 1020:     \ get linked-list
 1021:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1022:     \ mark ghost as resolved
 1023:     dup r@ >link ! <res> r@ >magic !
 1024:     \ loop through forward referencies
 1025:     r> -rot 
 1026:     comp-state @ >r Resolving comp-state !
 1027:     resolve-loop 
 1028:     r> comp-state !
 1029: 
 1030:     ['] noop IS resolve-warning 
 1031:   ;
 1032: 
 1033: \ gexecute ghost,                                      01nov92py
 1034: 
 1035: : is-forward   ( ghost -- )
 1036:   colonmark, 0 (refered) ; \ compile space for call
 1037: 
 1038: : is-resolved   ( ghost -- )
 1039:   >link @ colon, ; \ compile-call
 1040: 
 1041: : gexecute   ( ghost -- )
 1042:   dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;
 1043: 
 1044: : addr,  ( ghost -- )
 1045:   dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1046: 
 1047: \ !! : ghost,     ghost  gexecute ;
 1048: 
 1049: \ .unresolved                                          11may93jaw
 1050: 
 1051: variable ResolveFlag
 1052: 
 1053: \ ?touched                                             11may93jaw
 1054: 
 1055: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1056:                                0 <> and ;
 1057: 
 1058: : .forwarddefs ( ghost -- )
 1059: 	."  appeared in:"
 1060: 	>link
 1061: 	BEGIN	@ dup
 1062: 	WHILE	cr 5 spaces
 1063: 		dup >ghost @ >ghostname type
 1064: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1065: 		."  line " dup >line @ .dec
 1066: 	REPEAT 
 1067: 	drop ;
 1068: 
 1069: : ?resolved  ( ghostname -- )
 1070:   dup cell+ @ ?touched
 1071:   IF  	dup 
 1072: 	cell+ cell+ count cr type ResolveFlag on 
 1073: 	cell+ @ .forwarddefs
 1074:   ELSE 	drop 
 1075:   THEN ;
 1076: 
 1077: >MINIMAL
 1078: : .unresolved  ( -- )
 1079:   ResolveFlag off cr ." Unresolved: "
 1080:   Ghostnames
 1081:   BEGIN @ dup
 1082:   WHILE dup ?resolved
 1083:   REPEAT drop ResolveFlag @
 1084:   IF
 1085:       -1 abort" Unresolved words!"
 1086:   ELSE
 1087:       ." Nothing!"
 1088:   THEN
 1089:   cr ;
 1090: 
 1091: : .stats
 1092:   base @ >r decimal
 1093:   cr ." named Headers: " headers-named @ . 
 1094:   r> base ! ;
 1095: 
 1096: >CROSS
 1097: \ Header states                                        12dec92py
 1098: 
 1099: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
 1100: 
 1101: VARIABLE ^imm
 1102: 
 1103: >TARGET
 1104: : immediate     40 flag!
 1105:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1106:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1107:                 <imm> ^imm @ ! ;
 1108: : restrict      20 flag! ;
 1109: 
 1110: : isdoer	
 1111: \G define a forth word as doer, this makes obviously only sence on
 1112: \G forth processors such as the PSC1000
 1113: 		<do:> last-header-ghost @ >magic ! ;
 1114: >CROSS
 1115: 
 1116: \ ALIAS2 ansforth conform alias                          9may93jaw
 1117: 
 1118: : ALIAS2 create here 0 , DOES> @ execute ;
 1119: \ usage:
 1120: \ ' <name> alias2 bla !
 1121: 
 1122: \ Target Header Creation                               01nov92py
 1123: 
 1124: >TARGET
 1125: : string,  ( addr count -- )
 1126:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
 1127: : name,  ( "name" -- )  bl word count T string, cfalign H ;
 1128: : view,   ( -- ) ( dummy ) ;
 1129: >CROSS
 1130: 
 1131: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 1132: 
 1133: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 1134: \ contains the file-id of the documentation file
 1135: 
 1136: : T-\G ( -- )
 1137:     source >in @ /string doc-file-id write-line throw
 1138:     postpone \ ;
 1139: 
 1140: Variable to-doc  to-doc on
 1141: 
 1142: : cross-doc-entry  ( -- )
 1143:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 1144:     IF
 1145: 	s" " doc-file-id write-line throw
 1146: 	s" make-doc " doc-file-id write-file throw
 1147: 	tlast @ >image count $1F and doc-file-id write-file throw
 1148: 	>in @
 1149: 	[char] ( parse 2drop
 1150: 	[char] ) parse doc-file-id write-file throw
 1151: 	s"  )" doc-file-id write-file throw
 1152: 	[char] \ parse 2drop					
 1153: 	T-\G
 1154: 	>in !
 1155:     THEN ;
 1156: 
 1157: \ Target TAGS creation
 1158: 
 1159: s" kernel.TAGS" r/w create-file throw value tag-file-id
 1160: \ contains the file-id of the tags file
 1161: 
 1162: Create tag-beg 2 c,  7F c, bl c,
 1163: Create tag-end 2 c,  bl c, 01 c,
 1164: Create tag-bof 1 c,  0C c,
 1165: 
 1166: 2variable last-loadfilename 0 0 last-loadfilename 2!
 1167: 	    
 1168: : put-load-file-name ( -- )
 1169:     loadfilename 2@ last-loadfilename 2@ d<>
 1170:     IF
 1171: 	tag-bof count tag-file-id write-line throw
 1172: 	sourcefilename 2dup
 1173: 	tag-file-id write-file throw
 1174: 	last-loadfilename 2!
 1175: 	s" ,0" tag-file-id write-line throw
 1176:     THEN ;
 1177: 
 1178: : cross-tag-entry  ( -- )
 1179:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 1180:     IF
 1181: 	put-load-file-name
 1182: 	source >in @ min tag-file-id write-file throw
 1183: 	tag-beg count tag-file-id write-file throw
 1184: 	tlast @ >image count $1F and tag-file-id write-file throw
 1185: 	tag-end count tag-file-id write-file throw
 1186: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 1187: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 1188: 	s" ,0" tag-file-id write-line throw
 1189: 	base !
 1190:     THEN ;
 1191: 
 1192: \ Check for words
 1193: 
 1194: Defer skip? ' false IS skip?
 1195: 
 1196: : skipdef ( <name> -- )
 1197: \G skip definition of an undefined word in undef-words mode
 1198:     ghost dup forward?
 1199:     IF  >magic <skip> swap !
 1200:     ELSE drop THEN ;
 1201: 
 1202: : defined? ( -- flag ) \ name
 1203:     ghost undefined? 0= ;
 1204: 
 1205: : defined2? ( -- flag ) \ name
 1206: \G return true for anything else than forward, even for <skip>
 1207: \G that's what we want
 1208:     ghost forward? 0= ;
 1209: 
 1210: : needed? ( -- flag ) \ name
 1211: \G returns a false flag when
 1212: \G a word is not defined
 1213: \G a forward reference exists
 1214: \G so the definition is not skipped!
 1215:     bl word gfind
 1216:     IF dup undefined?
 1217: 	nip
 1218: 	0=
 1219:     ELSE  drop true  THEN ;
 1220: 
 1221: : doer? ( -- flag ) \ name
 1222:     ghost >magic @ <do:> = ;
 1223: 
 1224: : skip-defs ( -- )
 1225:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 1226: 
 1227: \ Target header creation
 1228: 
 1229: Variable CreateFlag
 1230: CreateFlag off
 1231: 
 1232: Variable NoHeaderFlag
 1233: NoHeaderFlag off
 1234: 
 1235: : 0.r ( n1 n2 -- ) 
 1236:     base @ >r hex 
 1237:     0 swap <# 0 ?DO # LOOP #> type 
 1238:     r> base ! ;
 1239: : .sym
 1240:   bounds 
 1241:   DO I c@ dup
 1242: 	CASE	'/ OF drop ." \/" ENDOF
 1243: 		'\ OF drop ." \\" ENDOF
 1244: 		dup OF emit ENDOF
 1245: 	ENDCASE
 1246:     LOOP ;
 1247: 
 1248: : (Theader ( "name" -- ghost )
 1249:     \  >in @ bl word count type 2 spaces >in !
 1250:     \ wordheaders will always be compiled to rom
 1251:     switchrom
 1252:     \ build header in target
 1253:     NoHeaderFlag @
 1254:     IF  NoHeaderFlag off
 1255:     ELSE
 1256: 	T align H view,
 1257: 	tlast @ dup 0> IF  T 1 cells - H THEN T A, H  there tlast !
 1258: 	1 headers-named +!	\ Statistic
 1259: 	>in @ T name, H >in !
 1260:     THEN
 1261:     T cfalign here H tlastcfa !
 1262:     \ Symbol table
 1263: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 1264:     CreateFlag @
 1265:     IF
 1266: 	>in @ alias2 swap >in !         \ create alias in target
 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>