Annotation of gforth/cross.fs, revision 1.64

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

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