File:  [gforth] / gforth / cross.fs
Revision 1.67: download - view: text, annotated - select for diffs
Fri Feb 19 18:25:28 1999 UTC (21 years ago) by jwilke
Branches: MAIN
CVS tags: HEAD
Updated cross.fs:
region interface supports now diffrent memory regions in a greater address-space
than we have memory.
image, bit$ and makekernel will get obsolete, use region interface instead.

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

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