Annotation of gforth/cross.fs, revision 1.54

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

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