File:  [gforth] / gforth / cross.fs
Revision 1.70: download - view: text, annotated - select for diffs
Mon Feb 22 17:40:18 1999 UTC (25 years, 1 month ago) by jwilke
Branches: MAIN
CVS tags: HEAD
Replaces bits/byte by bits/char. EC-targets currently broken.

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

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