File:  [gforth] / gforth / cross.fs
Revision 1.73: download - view: text, annotated - select for diffs
Wed May 5 12:07:55 1999 UTC (24 years, 11 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
new compile flag: stantardthreading

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

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