File:  [gforth] / gforth / cross.fs
Revision 1.74: download - view: text, annotated - select for diffs
Wed May 5 18:07:51 1999 UTC (24 years, 11 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
Added nice debugging flags support.

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

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