cross.fs 46.5 KB
Newer Older
anton's avatar
anton committed
1 2
\ CROSS.FS     The Cross-Compiler                      06oct92py
\ Idea and implementation: Bernd Paysan (py)
anton's avatar
anton committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

\ Copyright (C) 1995 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation; either version 2
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program; if not, write to the Free Software
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
anton's avatar
anton committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

\ Log:
\       changed in ; [ to state off           12may93jaw
\       included place +place                 12may93jaw
\       for a created word (variable, constant...)
\       is now an alias in the target voabulary.
\       this means it is no longer necessary to
\       switch between vocabularies for variable
\       initialization                        12may93jaw
\       discovered error in DOES>
\       replaced !does with (;code)           16may93jaw
\       made complete redesign and
\       introduced two vocs method
\       to be asure that the right words
\       are found                             08jun93jaw
\       btw:  ! works not with 16 bit
\             targets                         09jun93jaw
\       added: 2user and value                11jun93jaw

anton's avatar
anton committed
40 41 42
\ 	needed? works better now!!!		01mar97jaw
\	mach file is only loaded into target
\	cell corrected
jwilke's avatar
jwilke committed
43
\ 	romable extansions			27apr97-5jun97jaw
anton's avatar
anton committed
44 45


jwilke's avatar
jwilke committed
46 47 48 49 50 51 52 53 54 55
hex     \ the defualt base for the cross-compiler is hex !!
Warnings off

\ words that are generaly useful

: >wordlist ( vocabulary-xt -- wordlist-struct )
  also execute get-order swap >r 1- set-order r> ;

: umax 2dup u< IF swap THEN drop ;
: umin 2dup u> IF swap THEN drop ;
anton's avatar
anton committed
56

pazsan's avatar
pazsan committed
57 58 59
: string, ( c-addr u -- )
    \ puts down string as cstring
    dup c, here swap chars dup allot move ;
60

jwilke's avatar
jwilke committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74
: SetValue ( n -- <name> )
\G Same behaviour as "Value" when the <name> is not defined
\G Same behaviour as "to" when <name> is defined
\G SetValue searches in the current vocabulary
 save-input bl word >r restore-input throw r> count
 get-current search-wordlist
 IF bl word drop >body ! ELSE Value THEN ;

: DefaultValue ( n -- <name> )
\G Same behaviour as "Value" when the <name> is not defined
\G SetValue searches in the current vocabulary
 save-input bl word >r restore-input throw r> count
 get-current search-wordlist
 IF bl word drop drop drop ELSE Value THEN ;
anton's avatar
anton committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

hex

Vocabulary Cross
Vocabulary Target
Vocabulary Ghosts
VOCABULARY Minimal
only Forth also Target also also
definitions Forth

: T  previous Cross also Target ; immediate
: G  Ghosts ; immediate
: H  previous Forth also Cross ; immediate

forth definitions

: T  previous Cross also Target ; immediate
: G  Ghosts ; immediate

: >cross  also Cross definitions previous ;
: >target also Target definitions previous ;
: >minimal also Minimal definitions previous ;

H

>CROSS

jwilke's avatar
jwilke committed
102 103 104
\ 1 Constant Cross-Flag	\ to check whether assembler compiler plug-ins are
			\ for cross-compiling
\ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
pazsan's avatar
pazsan committed
105

jwilke's avatar
jwilke committed
106 107 108 109 110
: comment? ( c-addr u -- c-addr u )
        2dup s" (" compare 0=
        IF    postpone (
        ELSE  2dup s" \" compare 0= IF postpone \ THEN
        THEN ;
pazsan's avatar
pazsan committed
111

jwilke's avatar
jwilke committed
112
\ Begin CROSS COMPILER:
anton's avatar
anton committed
113

pazsan's avatar
pazsan committed
114 115


jwilke's avatar
jwilke committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
\ \ --------------------        Error Handling                  05aug97jaw

\ Flags

also forth definitions  \ these values may be predefined before
                        \ the cross-compiler is loaded

false DefaultValue stack-warn   	 \ check on empty stack at any definition
false DefaultValue create-forward-warn   \ warn on forward declaration of created words

[IFUNDEF] DebugMaskSrouce Variable DebugMaskSource 0 DebugMaskSource ! [THEN]
[IFUNDEF] DebugMaskCross  Variable DebugMaskCross  0 DebugMaskCross  ! [THEN]

previous >CROSS

: .sourcepos
  cr sourcefilename type ." :"
  base @ decimal sourceline# . base ! ;

: warnhead
\G display error-message head
\G perhaps with linenumber and filename
  .sourcepos ." Warning: " ;

: empty? depth IF .sourcepos ." Stack not empty!"  THEN ;

stack-warn [IF]
: defempty? empty? ;
[ELSE]
: defempty? ; immediate
[THEN]


\ \ GhostNames Ghosts                                  9may93jaw

\ second name source to search trough list

VARIABLE GhostNames
0 GhostNames !

: GhostName ( -- addr )
    here GhostNames @ , GhostNames ! here 0 ,
    bl word count
    \ 2dup type space
    string, \ !! cfalign ?
    align ;

\ Ghost Builder                                        06oct92py

\ <T T> new version with temp variable                 10may93jaw

VARIABLE VocTemp

: <T  get-current VocTemp ! also Ghosts definitions ;
: T>  previous VocTemp @ set-current ;

pazsan's avatar
pazsan committed
172
hex
jwilke's avatar
jwilke committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
4711 Constant <fwd>             4712 Constant <res>
4713 Constant <imm>             4714 Constant <do:>

\ iForth makes only immediate directly after create
\ make atonce trick! ?

Variable atonce atonce off

: NoExec true ABORT" CROSS: Don't execute ghost" ;

: GhostHeader <fwd> , 0 , ['] NoExec , ;

: >magic ;		\ type of ghost
: >link cell+ ;		\ pointer where ghost is in target, or if unresolved
			\ points to the where we have to resolve (linked-list)
: >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
: >end 3 cells + ;	\ room for additional tags
			\ for builder (create, variable...) words the
			\ execution symantics of words built are placed here

Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
Variable last-ghost	\ last ghost that is created
Variable last-header-ghost \ last ghost definitions with header

: Make-Ghost ( "name" -- ghost )
  >in @ GhostName swap >in !
  <T Create atonce @ IF immediate atonce off THEN
  here tuck swap ! ghostheader T>
  dup last-ghost !
  DOES> dup executed-ghost ! >exec @ execute ;

\ ghost words                                          14oct92py
\                                          changed:    10may93py/jaw

: gfind   ( string -- ghost true/1 / string false )
\ searches for string in word-list ghosts
  dup count [ ' ghosts >wordlist ] ALiteral search-wordlist
  dup IF >r >body nip r>  THEN ;

: gdiscover ( xt -- ghost true | xt false )
  GhostNames
  BEGIN @ dup
  WHILE 2dup
        cell+ @ dup >magic @ <fwd> <>
        >r >link @ = r> and
        IF cell+ @ nip true EXIT THEN
  REPEAT
  drop false ;

VARIABLE Already

: ghost   ( "name" -- ghost )
  Already off
  >in @  bl word gfind   IF  Already on nip EXIT  THEN
  drop  >in !  Make-Ghost ;

: >ghostname ( ghost -- adr len )
  GhostNames
  BEGIN @ dup
  WHILE 2dup cell+ @ =
  UNTIL nip 2 cells + count
  ELSE  2drop true abort" CROSS: Ghostnames inconsistent"
  THEN ;

' >ghostname ALIAS @name

: forward? ( ghost -- flag )
  >magic @ <fwd> = ;

\ Predefined ghosts                                    12dec92py

ghost 0=                                        drop
ghost branch    ghost ?branch                   2drop
ghost (do)      ghost (?do)                     2drop
ghost (for)                                     drop
ghost (loop)    ghost (+loop)                   2drop
ghost (next)                                    drop
ghost unloop    ghost ;S                        2drop
ghost lit       ghost (compile) ghost !         2drop drop
ghost (does>)   ghost noop                      2drop
ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
ghost '                                         drop
ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
ghost over      ghost =         ghost drop      2drop drop
ghost - drop

\ \ Parameter for target systems                         06oct92py

\ we define it ans like...
wordlist Constant target-environment

VARIABLE env-current \ save information of current dictionary to restore with environ>

: >ENVIRON get-current env-current ! target-environment set-current ;
: ENVIRON> env-current @ set-current ; 

>TARGET

: environment?
  target-environment search-wordlist 
  IF execute true ELSE false THEN ;

: e? name T environment? H 0= ABORT" environment variable not defined!" ;

: has? name T environment? H IF ELSE false THEN ;

: $has? T environment? H IF ELSE false THEN ;

>ENVIRON
true Value cross
>TARGET

mach-file count included hex

>TARGET

[IFUNDEF] has-interpreter true Value has-interpreter [THEN]
[IFUNDEF] itc true Value itc [THEN]
[IFUNDEF] has-rom false Value has-rom [THEN]
pazsan's avatar
pazsan committed
292 293 294

>CROSS

jwilke's avatar
jwilke committed
295
\ \ Create additional parameters                         19jan95py
pazsan's avatar
pazsan committed
296 297

T
anton's avatar
anton committed
298
NIL		   Constant TNIL
pazsan's avatar
pazsan committed
299 300 301 302 303 304 305 306
cell               Constant tcell
cell<<             Constant tcell<<
cell>bit           Constant tcell>bit
bits/byte          Constant tbits/byte
float              Constant tfloat
1 bits/byte lshift Constant maxbyte
H

anton's avatar
anton committed
307 308 309 310 311 312 313 314
\ Variables                                            06oct92py

Variable image
Variable tlast    TNIL tlast !  \ Last name field
Variable tlastcfa \ Last code field
Variable tdoes    \ Resolve does> calls
Variable bit$

jwilke's avatar
jwilke committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
\ statistics						10jun97jaw

Variable headers-named 0 headers-named !
Variable user-vars 0 user-vars !

\ Memory initialisation                                05dec92py

[IFDEF] Memory \ Memory is a bigFORTH feature
   also Memory
   : initmem ( var len -- )
     2dup swap handle! >r @ r> erase ;
   toss
[ELSE]
   : initmem ( var len -- )
     tuck allocate abort" CROSS: No memory for target"
     ( len var adr ) dup rot !
     ( len adr ) swap erase ;
[THEN]

\ MakeKernal                                           12dec92py

: makekernel ( targetsize -- targetsize )
  bit$  over 1- tcell>bit rshift 1+ initmem
  image over initmem ;

>MINIMAL
: makekernel makekernel ;


>CROSS

\ memregion.fs


Variable last-defined-region    \ pointer to last defined region
Variable region-link            \ linked list with all regions
Variable mirrored-link          \ linked list for mirrored regions
0 dup mirrored-link ! region-link !


: >rdp 2 cells + ;
: >rlen cell+ ;
: >rstart ;


: region ( addr len -- )                \G create a new region
  \ check whether predefined region exists 
  save-input bl word find >r >r restore-input throw r> r> 0= 
  IF	\ make region
	drop
	save-input create restore-input throw
	here last-defined-region !
	over ( startaddr ) , ( length ) , ( dp ) ,
	region-link linked name string,
  ELSE	\ store new parameters in region
        bl word drop
	>body >r r@ last-defined-region !
	r@ cell+ ! dup r@ ! r> 2 cells + !
  THEN ;

: borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  dup @ swap cell+ @ over + ;

: extent  ( region -- startaddr len )   \G returns the really used area
  dup @ swap 2 cells + @ over - ;

: area ( region -- startaddr totallen ) \G returns the total area
  dup @ swap cell+ @ ;

: mirrored                              \G mark a region as mirrored
  mirrored-link
  linked last-defined-region @ , ;

: .addr
  base @ >r hex
  tcell 2 u>
  IF s>d <# # # # # '. hold # # # # #> type
  ELSE s>d <# # # # # # #> type
  THEN r> base ! ;

: .regions                      \G display region statistic

  \ we want to list the regions in the right order
  \ so first collect all regions on stack
  0 region-link @
  BEGIN dup WHILE dup @ REPEAT drop
  BEGIN dup
  WHILE cr 3 cells - >r
	r@ 4 cells + count tuck type
        12 swap - 0 max spaces space
	." Start: " r@ @ dup .addr space
	." End: " r@ 1 cells + @ + .addr space
	." DP: " r> 2 cells + @ .addr 
  REPEAT drop
  s" rom" $has? 0= ?EXIT
  cr ." Mirrored:"
  mirrored-link @
  BEGIN dup
  WHILE	space dup cell+ @ 4 cells + count type @
  REPEAT drop cr
  ;

\ -------- predefined regions

0 0 region address-space
\ total memory addressed and used by the target system

0 0 region dictionary
\ rom area for the compiler

has? rom 
[IF]
0 0 region ram-dictionary mirrored
\ ram area for the compiler
[ELSE]
' dictionary ALIAS ram-dictionary
[THEN]

0 0 region return-stack

0 0 region data-stack

0 0 region tib-region

' dictionary ALIAS rom-dictionary


: setup-target ( -- )   \G initialize targets memory space
  s" rom" $has?
  IF  \ check for ram and rom...
      address-space area nip
      ram-dictionary area nip
      rom-dictionary area nip
      and and 0=
      ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
  THEN
  address-space area nip
  IF
      address-space area
  ELSE
      dictionary area
  THEN
  dup 0=
  ABORT" CROSS: define at least address-space or dictionary!!"
  + makekernel drop ;

\ switched tdp for rom support				03jun97jaw

\ second value is here to store some maximal value for statistics
\ tempdp is also embedded here but has nothing to do with rom support
\ (needs switched dp)

variable tempdp	0 ,	\ temporary dp for resolving
variable tempdp-save

0 [IF]
variable romdp 0 ,      \ Dictionary-Pointer for ramarea
variable ramdp 0 ,      \ Dictionary-Pointer for romarea

\
variable sramdp		\ start of ram-area for forth
variable sromdp		\ start of rom-area for forth

[THEN]


0 value tdp
variable fixed		\ flag: true: no automatic switching
			\	false: switching is done automatically

\ Switch-Policy:
\
\ a header is always compiled into rom
\ after a created word (create and variable) compilation goes to ram
\
\ Be careful: If you want to make the data behind create into rom
\ you have to put >rom before create!

variable constflag constflag off

: (switchram)
  fixed @ ?EXIT has-rom 0= ?EXIT
  ram-dictionary >rdp to tdp ;

: switchram
  constflag @
  IF constflag off ELSE (switchram) THEN ;

: switchrom
  fixed @ ?EXIT rom-dictionary >rdp to tdp ;

: >tempdp ( addr -- ) 
  tdp tempdp-save ! tempdp to tdp tdp ! ;
: tempdp> ( -- )
  tempdp-save @ to tdp ;

: >ram  fixed off (switchram) fixed on ;
: >rom  fixed off switchrom fixed on ;
: >auto fixed off switchrom ;



\ : romstart dup sromdp ! romdp ! ;
\ : ramstart dup sramdp ! ramdp ! ;

\ default compilation goed to rom
\ when romable support is off, only the rom switch is used (!!)
>auto

: there  tdp @ ;
anton's avatar
anton committed
525

anton's avatar
anton committed
526 527
>TARGET

jwilke's avatar
jwilke committed
528 529
\ \ Target Memory Handling

anton's avatar
anton committed
530 531
\ Byte ordering and cell size                          06oct92py

pazsan's avatar
pazsan committed
532 533
: cell+         tcell + ;
: cells         tcell<< lshift ;
anton's avatar
anton committed
534
: chars         ;
anton's avatar
anton committed
535
: char+		1 + ;
pazsan's avatar
pazsan committed
536
: floats	tfloat * ;
537
    
anton's avatar
anton committed
538
>CROSS
pazsan's avatar
pazsan committed
539
: cell/         tcell<< rshift ;
anton's avatar
anton committed
540 541
>TARGET
20 CONSTANT bl
jwilke's avatar
jwilke committed
542
\ TNIL Constant NIL
anton's avatar
anton committed
543 544 545

>CROSS

546 547
bigendian
[IF]
jwilke's avatar
jwilke committed
548
   : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
549
     DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
jwilke's avatar
jwilke committed
550
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds
551
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
pazsan's avatar
pazsan committed
552
[ELSE]
jwilke's avatar
jwilke committed
553
   : S!  ( n addr -- )  >r s>d r> tcell bounds
554
     DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
jwilke's avatar
jwilke committed
555
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
556
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
anton's avatar
anton committed
557 558 559 560 561 562 563 564 565 566
[THEN]

>CROSS
\ Bit string manipulation                               06oct92py
\                                                       9may93jaw
CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
: bits ( n -- n ) chars Bittable + c@ ;

: >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
: +bit ( addr n -- )  >bit over c@ or swap c! ;
pazsan's avatar
pazsan committed
567
: -bit ( addr n -- )  >bit invert over c@ and swap c! ;
anton's avatar
anton committed
568
: relon ( taddr -- )  bit$ @ swap cell/ +bit ;
pazsan's avatar
pazsan committed
569
: reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
anton's avatar
anton committed
570 571 572 573

\ Target memory access                                 06oct92py

: align+  ( taddr -- rest )
anton's avatar
anton committed
574
    tcell tuck 1- and - [ tcell 1- ] Literal and ;
575
: cfalign+  ( taddr -- rest )
pazsan's avatar
pazsan committed
576
    \ see kernel.fs:cfaligned
pazsan's avatar
pazsan committed
577
    /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
anton's avatar
anton committed
578 579 580 581 582

>TARGET
: aligned ( taddr -- ta-addr )  dup align+ + ;
\ assumes cell alignment granularity (as GNU C)

583
: cfaligned ( taddr1 -- taddr2 )
pazsan's avatar
pazsan committed
584
    \ see kernel.fs
585 586
    dup cfalign+ + ;

anton's avatar
anton committed
587 588 589
>CROSS
: >image ( taddr -- absaddr )  image @ + ;
>TARGET
jwilke's avatar
jwilke committed
590 591
: @  ( taddr -- w )     >image S@ ;
: !  ( w taddr -- )     >image S! ;
anton's avatar
anton committed
592 593
: c@ ( taddr -- char )  >image c@ ;
: c! ( char taddr -- )  >image c! ;
594 595
: 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
: 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
anton's avatar
anton committed
596 597 598 599 600 601

\ Target compilation primitives                        06oct92py
\ included A!                                          16may93jaw

: here  ( -- there )    there ;
: allot ( n -- )        tdp +! ;
jwilke's avatar
jwilke committed
602
: ,     ( w -- )        T here H tcell T allot  ! H T here drop H ;
anton's avatar
anton committed
603 604
: c,    ( char -- )     T here    1 allot c! H ;
: align ( -- )          T here H align+ 0 ?DO  bl T c, H LOOP ;
605 606
: cfalign ( -- )
    T here H cfalign+ 0 ?DO  bl T c, H LOOP ;
anton's avatar
anton committed
607 608 609 610 611 612

: A!                    dup relon T ! H ;
: A,    ( w -- )        T here H relon T , H ;

>CROSS

jwilke's avatar
jwilke committed
613 614 615 616 617
: tcmove ( source dest len -- )
\G cmove in target memory
  bounds
  ?DO  dup T c@ H I T c! H 1+
  LOOP  drop ;
anton's avatar
anton committed
618 619

>TARGET
jwilke's avatar
jwilke committed
620
H also Forth definitions \ ." asm: " order
anton's avatar
anton committed
621

jwilke's avatar
jwilke committed
622 623 624 625 626 627
: X 	also target bl word find
	IF	state @ IF compile,
		ELSE execute THEN
	ELSE	previous ABORT" Cross: access method not supported!"
	THEN 
	previous ; immediate
anton's avatar
anton committed
628

jwilke's avatar
jwilke committed
629
[IFDEF] asm-include asm-include [THEN] hex
anton's avatar
anton committed
630

jwilke's avatar
jwilke committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
previous
>CROSS H

\ \ --------------------        Compiler Plug Ins               01aug97jaw

Defer lit, ( n -- )
Defer alit, ( n -- )
Defer branch, ( target-addr -- )
Defer ?branch, ( target-addr -- )
Defer branchmark, ( -- branch-addr )
Defer ?branchmark, ( -- branch-addr )
Defer branchto,
Defer branchtoresolve, ( branch-addr -- )
Defer branchfrom, ( -- )
Defer branchtomark, ( -- target-addr )
Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
Defer colon-resolve ( tcfa addr -- )
Defer addr-resolve ( target-addr addr -- )
anton's avatar
anton committed
649

jwilke's avatar
jwilke committed
650 651 652
[IFUNDEF] ca>native
defer ca>native	
[THEN]
anton's avatar
anton committed
653

jwilke's avatar
jwilke committed
654 655 656 657 658 659 660 661 662 663 664
>TARGET
DEFER >body             \ we need the system >body
			\ and the target >body
>CROSS
T 2 cells H VALUE xt>body
DEFER doprim,
DEFER docol,     \ compiles start of definition and doer
DEFER doer,		
DEFER fini,      \ compiles end of definition ;s
DEFER doeshandler,
DEFER dodoes,
anton's avatar
anton committed
665

jwilke's avatar
jwilke committed
666 667
DEFER ]comp     \ starts compilation
DEFER comp[     \ ends compilation
anton's avatar
anton committed
668

jwilke's avatar
jwilke committed
669 670 671
: (cc) T a, H ;			' (cc) IS colon,
: (cr) >tempdp ]comp colon, comp[ tempdp> ; ' (cr) IS colon-resolve
: (ar) T ! H ;			' (ar) IS addr-resolve
anton's avatar
anton committed
672

jwilke's avatar
jwilke committed
673 674 675
>TARGET
: compile, colon, ;
>CROSS
anton's avatar
anton committed
676 677 678



jwilke's avatar
jwilke committed
679
\ resolve structure
anton's avatar
anton committed
680

jwilke's avatar
jwilke committed
681 682 683 684
: >next ;		\ link to next field
: >tag cell+ ;		\ indecates type of reference: 0: call, 1: address
: >taddr cell+ cell+ ;
: >ghost 3 cells + ;
anton's avatar
anton committed
685

jwilke's avatar
jwilke committed
686 687 688
: refered ( ghost tag -- )
  swap >r here r@ >link @ , r@ >link ! ( tag ) ,
  T here aligned H , r> drop  last-header-ghost @ , ;
anton's avatar
anton committed
689

jwilke's avatar
jwilke committed
690
Defer resolve-warning
anton's avatar
anton committed
691

jwilke's avatar
jwilke committed
692 693
: reswarn-test ( ghost res-struct -- ghost res-struct )
  over cr ." Resolving " >ghostname type dup ."  in " >ghost @ >ghostname type ;
anton's avatar
anton committed
694

jwilke's avatar
jwilke committed
695 696 697
: reswarn-forward ( ghost res-struct -- ghost res-struct )
  over warnhead >ghostname type dup ."  is referenced in " 
  >ghost @ >ghostname type ;
anton's avatar
anton committed
698

jwilke's avatar
jwilke committed
699 700
\ ' reswarn-test IS resolve-warning
 
anton's avatar
anton committed
701 702
\ resolve                                              14oct92py

jwilke's avatar
jwilke committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716
 : resolve-loop ( ghost tcfa -- ghost tcfa )
  >r dup >link
  BEGIN @ dup WHILE 
	resolve-warning 
	r@ over >taddr @ 
	2 pick >tag @
	IF	addr-resolve
	ELSE	colon-resolve
	THEN
  REPEAT drop r> ;

\ : resolve-loop ( ghost tcfa -- ghost tcfa )
\  >r dup >link @
\  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
anton's avatar
anton committed
717 718 719

\ exists                                                9may93jaw

jwilke's avatar
jwilke committed
720 721 722 723 724
Variable TWarnings
TWarnings on
Variable Exists-Warnings
Exists-Warnings on

anton's avatar
anton committed
725 726 727 728 729
: exists ( ghost tcfa -- )
  over GhostNames
  BEGIN @ dup
  WHILE 2dup cell+ @ =
  UNTIL
jwilke's avatar
jwilke committed
730 731 732 733 734
        2 cells + count
        TWarnings @ Exists-Warnings @ and
        IF warnhead type ."  exists"
        ELSE 2drop THEN
        drop swap >link !
pazsan's avatar
pazsan committed
735
  ELSE  true abort" CROSS: Ghostnames inconsistent "
anton's avatar
anton committed
736 737 738
  THEN ;

: resolve  ( ghost tcfa -- )
jwilke's avatar
jwilke committed
739 740 741 742 743
\ resolve referencies to ghost with tcfa
  over forward? 0= IF  exists EXIT THEN
  resolve-loop  over >link ! <res> swap >magic ! 
  ['] noop IS resolve-warning 
  ;
anton's avatar
anton committed
744 745 746

\ gexecute ghost,                                      01nov92py

jwilke's avatar
jwilke committed
747 748 749 750 751 752 753 754 755 756 757 758
: is-forward   ( ghost -- )
\  >link dup @ there rot !  T  A,  H ;
  0 refered  -1 colon, ;

: is-resolved   ( ghost -- )
  >link @ colon, ; \ compile-call

: gexecute   ( ghost -- )
  dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;

: addr,  ( ghost -- )
  dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
anton's avatar
anton committed
759

jwilke's avatar
jwilke committed
760
\ !! : ghost,     ghost  gexecute ;
anton's avatar
anton committed
761 762 763 764 765 766 767

\ .unresolved                                          11may93jaw

variable ResolveFlag

\ ?touched                                             11may93jaw

jwilke's avatar
jwilke committed
768
: ?touched ( ghost -- flag ) dup forward? swap >link @
anton's avatar
anton committed
769 770 771 772 773 774 775 776 777 778 779 780
                               0 <> and ;

: ?resolved  ( ghostname -- )
  dup cell+ @ ?touched
  IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;

>MINIMAL
: .unresolved  ( -- )
  ResolveFlag off cr ." Unresolved: "
  Ghostnames
  BEGIN @ dup
  WHILE dup ?resolved
anton's avatar
anton committed
781 782
  REPEAT drop ResolveFlag @
  IF
anton's avatar
anton committed
783
      -1 abort" Unresolved words!"
anton's avatar
anton committed
784 785 786 787
  ELSE
      ." Nothing!"
  THEN
  cr ;
anton's avatar
anton committed
788

jwilke's avatar
jwilke committed
789 790 791 792 793 794 795
: .stats
  base @ >r decimal
  cr ." named Headers: " headers-named @ . 
\  cr ." MaxRam*" ramdp @ . 
\  cr ." MaxRom*" romdp @ . 
  r> base ! ;

anton's avatar
anton committed
796 797 798 799 800 801 802 803
>CROSS
\ Header states                                        12dec92py

: flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;

VARIABLE ^imm

>TARGET
804
: immediate     40 flag!
805
                ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
anton's avatar
anton committed
806 807
                <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                <imm> ^imm @ ! ;
808
: restrict      20 flag! ;
jwilke's avatar
jwilke committed
809 810

: isdoer	<do:> last-header-ghost @ >magic ! ;
anton's avatar
anton committed
811 812 813 814 815 816
>CROSS

\ ALIAS2 ansforth conform alias                          9may93jaw

: ALIAS2 create here 0 , DOES> @ execute ;
\ usage:
817
\ ' <name> alias2 bla !
anton's avatar
anton committed
818 819 820

\ Target Header Creation                               01nov92py

jwilke's avatar
jwilke committed
821
>TARGET
anton's avatar
anton committed
822
: string,  ( addr count -- )
pazsan's avatar
pazsan committed
823
  dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
jwilke's avatar
jwilke committed
824
: name,  ( "name" -- )  bl word count T string, cfalign H ;
anton's avatar
anton committed
825
: view,   ( -- ) ( dummy ) ;
jwilke's avatar
jwilke committed
826
>CROSS
anton's avatar
anton committed
827

828 829
\ Target Document Creation (goes to crossdoc.fd)       05jul95py

anton's avatar
anton committed
830
s" doc/crossdoc.fd" r/w create-file throw value doc-file-id
831 832
\ contains the file-id of the documentation file

pazsan's avatar
pazsan committed
833
: T-\G ( -- )
834
    source >in @ /string doc-file-id write-line throw
pazsan's avatar
pazsan committed
835
    postpone \ ;
836

pazsan's avatar
pazsan committed
837
Variable to-doc  to-doc on
838 839 840 841 842 843 844 845 846 847 848 849

: cross-doc-entry  ( -- )
    to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
    IF
	s" " doc-file-id write-line throw
	s" make-doc " doc-file-id write-file throw
	tlast @ >image count $1F and doc-file-id write-file throw
	>in @
	[char] ( parse 2drop
	[char] ) parse doc-file-id write-file throw
	s"  )" doc-file-id write-file throw
	[char] \ parse 2drop					
pazsan's avatar
pazsan committed
850
	T-\G
851
	>in !
pazsan's avatar
pazsan committed
852
    THEN ;
853

pazsan's avatar
pazsan committed
854 855
\ Target TAGS creation

pazsan's avatar
pazsan committed
856
s" kernel.TAGS" r/w create-file throw value tag-file-id
pazsan's avatar
pazsan committed
857 858 859 860 861 862 863 864 865 866 867 868
\ contains the file-id of the tags file

Create tag-beg 2 c,  7F c, bl c,
Create tag-end 2 c,  bl c, 01 c,
Create tag-bof 1 c,  0C c,

2variable last-loadfilename 0 0 last-loadfilename 2!
	    
: put-load-file-name ( -- )
    loadfilename 2@ last-loadfilename 2@ d<>
    IF
	tag-bof count tag-file-id write-line throw
869
	sourcefilename 2dup
pazsan's avatar
pazsan committed
870 871 872 873 874 875 876 877 878 879 880 881 882
	tag-file-id write-file throw
	last-loadfilename 2!
	s" ,0" tag-file-id write-line throw
    THEN ;

: cross-tag-entry  ( -- )
    tlast @ 0<>	\ not an anonymous (i.e. noname) header
    IF
	put-load-file-name
	source >in @ min tag-file-id write-file throw
	tag-beg count tag-file-id write-file throw
	tlast @ >image count $1F and tag-file-id write-file throw
	tag-end count tag-file-id write-file throw
883
	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
pazsan's avatar
pazsan committed
884 885 886 887 888
\	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
	s" ,0" tag-file-id write-line throw
	base !
    THEN ;

pazsan's avatar
pazsan committed
889 890 891 892 893
\ Check for words

Defer skip? ' false IS skip?

: defined? ( -- flag ) \ name
jwilke's avatar
jwilke committed
894
    ghost forward? 0= ;
pazsan's avatar
pazsan committed
895 896

: needed? ( -- flag ) \ name
anton's avatar
anton committed
897 898 899 900 901
\G returns a false flag when
\G a word is not defined
\G a forward reference exists
\G so the definition is not skipped!
    bl word gfind
jwilke's avatar
jwilke committed
902
    IF dup forward?
anton's avatar
anton committed
903 904 905
	nip
	0=
    ELSE  drop true  THEN ;
pazsan's avatar
pazsan committed
906

907 908 909
: doer? ( -- flag ) \ name
    ghost >magic @ <do:> = ;

pazsan's avatar
pazsan committed
910 911 912
: skip-defs ( -- )
    BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;

pazsan's avatar
pazsan committed
913 914
\ Target header creation

jwilke's avatar
jwilke committed
915

anton's avatar
anton committed
916 917
VARIABLE CreateFlag CreateFlag off

jwilke's avatar
jwilke committed
918 919 920 921 922 923 924 925 926 927
: 0.r ( n1 n2 -- ) 0 swap <# 0 ?DO # LOOP #> type ;
: .sym
  bounds 
  DO I c@ dup
	CASE	'/ OF drop ." \/" ENDOF
		'\ OF drop ." \\" ENDOF
		dup OF emit ENDOF
	ENDCASE
  LOOP ;

pazsan's avatar
pazsan committed
928 929
: (Theader ( "name" -- ghost )
\  >in @ bl word count type 2 spaces >in !
jwilke's avatar
jwilke committed
930 931
\ wordheaders will always be compiled to rom
  switchrom
pazsan's avatar
pazsan committed
932
  T align H view,
anton's avatar
anton committed
933
  tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
jwilke's avatar
jwilke committed
934 935 936 937
  1 headers-named +!	\ Statistic
  >in @ T name, H >in ! T here H tlastcfa !
  \ Symbol table
  \ >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
anton's avatar
anton committed
938
  CreateFlag @ IF
939 940 941 942
       >in @ alias2 swap >in !         \ create alias in target
       >in @ ghost swap >in !
       swap also ghosts ' previous swap !     \ tick ghost and store in alias
       CreateFlag off
anton's avatar
anton committed
943
  ELSE ghost THEN
jwilke's avatar
jwilke committed
944
  dup Last-Header-Ghost !
anton's avatar
anton committed
945 946 947
  dup >magic ^imm !     \ a pointer for immediate
  Already @ IF  dup >end tdoes !
  ELSE 0 tdoes ! THEN
948
  80 flag!
pazsan's avatar
pazsan committed
949
  cross-doc-entry cross-tag-entry ;
anton's avatar
anton committed
950 951

VARIABLE ;Resolve 1 cells allot
jwilke's avatar
jwilke committed
952 953
\ this is the resolver information from ":"
\ resolving is done by ";"
anton's avatar
anton committed
954

955 956
: Theader  ( "name" -- ghost )
  (THeader dup there resolve 0 ;Resolve ! ;
anton's avatar
anton committed
957 958 959

>TARGET
: Alias    ( cfa -- ) \ name
pazsan's avatar
pazsan committed
960 961 962 963 964 965
    >in @ skip? IF  2drop  EXIT  THEN  >in !
    dup 0< has-prims 0= and
    IF
	." needs prim: " >in @ bl word count type >in ! cr
    THEN
    (THeader over resolve T A, H 80 flag! ;
966
: Alias:   ( cfa -- ) \ name
pazsan's avatar
pazsan committed
967 968 969 970 971 972
    >in @ skip? IF  2drop  EXIT  THEN  >in !
    dup 0< has-prims 0= and
    IF
	." needs doer: " >in @ bl word count type >in ! cr
    THEN
    ghost tuck swap resolve <do:> swap >magic ! ;
anton's avatar
anton committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
>CROSS

\ Conditionals and Comments                            11may93jaw

: ;Cond
  postpone ;
  swap ! ;  immediate

: Cond: ( -- ) \ name {code } ;
  atonce on
  ghost
  >exec
  :NONAME ;

: restrict? ( -- )
\ aborts on interprete state - ae
  state @ 0= ABORT" CROSS: Restricted" ;

: Comment ( -- )
  >in @ atonce on ghost swap >in ! ' swap >exec ! ;

Comment (       Comment \

\ compile                                              10may93jaw

: compile  ( -- ) \ name
  restrict?
pazsan's avatar
pazsan committed
1000
  bl word gfind dup 0= ABORT" CROSS: Can't compile "
anton's avatar
anton committed
1001 1002 1003 1004 1005
  0> ( immediate? )
  IF    >exec @ compile,
  ELSE  postpone literal postpone gexecute  THEN ;
                                        immediate

jwilke's avatar
jwilke committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
: [G'] 
\G ticks a ghost and returns its address
  bl word gfind 0= ABORT" CROSS: Ghost don't exists"
  state @
  IF   postpone literal
  THEN ; immediate

: ghost>cfa
  dup forward? ABORT" CROSS: forward " >link @ ;
               
>TARGET

: '  ( -- cfa ) 
\ returns the target-cfa of a ghost
  bl word gfind 0= ABORT" CROSS: Ghost don't exists"
  ghost>cfa ;

Cond: [']  T ' H alit, ;Cond

>CROSS

: [T']
\ returns the target-cfa of a ghost, or compiles it as literal
  postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate

\ \ threading modell					13dec92py
\ modularized						14jun97jaw
1033

jwilke's avatar
jwilke committed
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
: fillcfa   ( usedcells -- )
  T cells H xt>body swap - 0 ?DO 0 T c, H LOOP ;

: (>body)   ( cfa -- pfa ) xt>body + ;		' (>body) T IS >body H

: (doer,)   ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ;   ' (doer,) IS doer,

: (docol,)  ( -- ) [G'] :docol doer, ;		' (docol,) IS docol,

: (doprim,) ( -- )
  there xt>body + ca>native T a, H 1 fillcfa ;	' (doprim,) IS doprim,

: (doeshandler,) ( -- ) 
  T cfalign H compile :doesjump T 0 , H ; 	' (doeshandler,) IS doeshandler,

: (dodoes,) ( does-action-ghost -- )
  ]comp [G'] :dodoes gexecute comp[
  addr,
  T here H tcell - reloff 2 fillcfa ;		' (dodoes,) IS dodoes,

: (lit,) ( n -- )   compile lit T  ,  H ;	' (lit,) IS lit,

: (alit,) ( n -- )  lit, T here cell - H relon ;	' (alit,) IS alit,

: (fini,)         compile ;s ;                ' (fini,) IS fini,
1059

pazsan's avatar
pazsan committed
1060 1061 1062 1063 1064
[IFUNDEF] (code) 
Defer (code)
Defer (end-code)
[THEN]

anton's avatar
anton committed
1065
>TARGET
pazsan's avatar
pazsan committed
1066
: Code
jwilke's avatar
jwilke committed
1067
  defempty?
anton's avatar
anton committed
1068 1069
  (THeader there resolve
  [ has-prims 0= [IF] ITC [ELSE] true [THEN] ] [IF]
jwilke's avatar
jwilke committed
1070
  doprim, 
anton's avatar
anton committed
1071 1072
  [THEN]
  depth (code) ;
pazsan's avatar
pazsan committed
1073 1074

: Code:
jwilke's avatar
jwilke committed
1075
  defempty?
anton's avatar
anton committed
1076
    ghost dup there ca>native resolve  <do:> swap >magic !
pazsan's avatar
pazsan committed
1077 1078 1079
    depth (code) ;

: end-code
jwilke's avatar
jwilke committed
1080
    (end-code)
pazsan's avatar
pazsan committed
1081 1082
    depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
    ELSE true ABORT" CROSS: Stack empty" THEN
jwilke's avatar
jwilke committed
1083
    ;
anton's avatar
anton committed
1084

1085 1086
Cond: chars ;Cond

anton's avatar
anton committed
1087 1088
>CROSS

jwilke's avatar
jwilke committed
1089
\ tLiteral                                             12dec92py
anton's avatar
anton committed
1090 1091

>TARGET
pazsan's avatar
pazsan committed
1092 1093
Cond: \G  T-\G ;Cond

anton's avatar
anton committed
1094 1095 1096 1097 1098 1099
Cond:  Literal ( n -- )   restrict? lit, ;Cond
Cond: ALiteral ( n -- )   restrict? alit, ;Cond

: Char ( "<char>" -- )  bl word char+ c@ ;
Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond

pazsan's avatar
pazsan committed
1100 1101
\ some special literals					27jan97jaw

jwilke's avatar
jwilke committed
1102 1103 1104
\ !! Known Bug: Special Literals and plug-ins work only correct
\ on 16 and 32 Bit Targets and 32 Bit Hosts!

pazsan's avatar
pazsan committed
1105
Cond: MAXU
jwilke's avatar
jwilke committed
1106 1107 1108 1109 1110
  restrict? 
  tcell 1 cells u> 
  IF	compile lit tcell 0 ?DO FF T c, H LOOP 
  ELSE	$ffffffff lit, THEN
  ;Cond
pazsan's avatar
pazsan committed
1111 1112

Cond: MINI
jwilke's avatar
jwilke committed
1113 1114 1115 1116 1117 1118 1119 1120
  restrict?
  tcell 1 cells u>
  IF	compile lit bigendian 
  	IF	80 T c, H tcell 1 ?DO 0 T c, H LOOP 
  	ELSE  	tcell 1 ?DO 0 T c, H LOOP 80 T c, H
  	THEN
  ELSE	tcell 2 = IF $8000 ELSE $80000000 THEN lit, THEN
  ;Cond
pazsan's avatar
pazsan committed
1121 1122
 
Cond: MAXI
jwilke's avatar
jwilke committed
1123 1124 1125 1126 1127 1128 1129
 restrict?
 tcell 1 cells u>
 IF	compile lit bigendian 
	IF 	7F T c, H tcell 1 ?DO FF T c, H LOOP
	ELSE 	tcell 1 ?DO FF T c, H LOOP 7F T c, H
 	THEN
 ELSE	tcell 2 = IF $7fff ELSE $7fffffff THEN lit, THEN
pazsan's avatar
pazsan committed
1130 1131
 ;Cond

anton's avatar
anton committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
>CROSS
\ Target compiling loop                                12dec92py
\ ">tib trick thrown out                               10may93jaw
\ number? defined at the top                           11may93jaw

\ compiled word might leave items on stack!
: tcom ( in name -- )
  gfind  ?dup  IF    0> IF    nip >exec @ execute
                        ELSE  nip gexecute  THEN EXIT THEN
  number? dup  IF    0> IF swap lit,  THEN  lit,  drop
               ELSE  2drop >in !
               ghost gexecute THEN  ;

>TARGET
\ : ; DOES>                                            13dec92py
\ ]                                                     9may93py/jaw

: ] state on
    BEGIN
pazsan's avatar
pazsan committed
1151
        BEGIN >in @ bl word
anton's avatar
anton committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
              dup c@ 0= WHILE 2drop refill 0=
              ABORT" CROSS: End of file while target compiling"
        REPEAT
        tcom
        state @
        0=
    UNTIL ;

\ by the way: defining a second interpreter (a compiler-)loop
\             is not allowed if a system should be ans conform

: : ( -- colon-sys ) \ Name
jwilke's avatar
jwilke committed
1164 1165 1166
  defempty?
  constflag off \ don't let this flag work over colon defs
		\ just to go sure nothing unwanted happens
pazsan's avatar
pazsan committed
1167
  >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
anton's avatar
anton committed
1168
  (THeader ;Resolve ! there ;Resolve cell+ !
jwilke's avatar
jwilke committed
1169
  docol, ]comp depth T ] H ;
anton's avatar
anton committed
1170

1171
: :noname ( -- colon-sys )
jwilke's avatar
jwilke committed
1172
  T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
1173

pazsan's avatar
pazsan committed
1174 1175
Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond

1176 1177
Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond

jwilke's avatar
jwilke committed
1178 1179 1180 1181 1182 1183 1184 1185
>CROSS
: LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
         ;Resolve cell+ @ ;

>TARGET

Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond

anton's avatar
anton committed
1186 1187 1188
Cond: ; ( -- ) restrict?
               depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                          ELSE true ABORT" CROSS: Stack empty" THEN
jwilke's avatar
jwilke committed
1189 1190 1191
               fini,
               comp[
               state off
anton's avatar
anton committed
1192 1193 1194 1195 1196 1197
               ;Resolve @
               IF ;Resolve @ ;Resolve cell+ @ resolve THEN
               ;Cond
Cond: [  restrict? state off ;Cond

>CROSS
jwilke's avatar
jwilke committed
1198 1199
: !does ( does-action -- )
\ !! zusammenziehen und dodoes, machen!
1200
    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
jwilke's avatar
jwilke committed
1201 1202
\ !! geht so nicht, da dodoes, ghost will!
\     tlastcfa @ >tempdp dodoes, tempdp> ;
anton's avatar
anton committed
1203 1204 1205

>TARGET
Cond: DOES> restrict?
jwilke's avatar
jwilke committed
1206 1207 1208
        compile (does>) doeshandler, 
	\ resolve words made by builders
	tdoes @ ?dup IF  @ T here H resolve THEN
anton's avatar
anton committed
1209
        ;Cond
jwilke's avatar
jwilke committed
1210
: DOES> switchrom doeshandler, T here H !does depth T ] H ;
anton's avatar
anton committed
1211 1212 1213 1214 1215 1216

>CROSS
\ Creation                                             01nov92py

\ Builder                                               11may93jaw

jwilke's avatar
jwilke committed
1217 1218 1219 1220 1221 1222
: Builder    ( Create-xt do:-xt "name" -- )
\ builds up a builder in current vocabulary
\ create-xt is executed when word is interpreted
\ do:-xt is executet when the created word from builder is executed
\ for do:-xt an additional entry after the normal ghost-enrys is used

anton's avatar
anton committed
1223 1224 1225
  >in @ alias2 swap dup >in ! >r >r
  Make-Ghost rot swap >exec ! ,
  r> r> >in !
1226 1227
  also ghosts ' previous swap ! ;
\  DOES>  dup >exec @ execute ;
anton's avatar
anton committed
1228

jwilke's avatar
jwilke committed
1229 1230 1231 1232
: gdoes,  ( ghost -- )
\ makes the codefield for a word that is built
  >end @ dup forward? 0=
  IF
1233
	dup >magic @ <do:> =
jwilke's avatar
jwilke committed
1234 1235 1236 1237 1238 1239
	IF  doer, EXIT THEN
  THEN
\  compile :dodoes gexecute
\  T here H tcell - reloff 
  dodoes,
;
anton's avatar
anton committed
1240

jwilke's avatar
jwilke committed
1241 1242
: TCreate ( <name> -- )
  executed-ghost @
anton's avatar
anton committed
1243
  CreateFlag on
jwilke's avatar
jwilke committed
1244 1245
  create-forward-warn
  IF ['] reswarn-forward IS resolve-warning THEN
1246
  Theader >r dup gdoes,
jwilke's avatar
jwilke committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
\ stores execution symantic in the built word
  >end @ >exec @ r> >exec ! ;

: RTCreate ( <name> -- )
\ creates a new word with code-field in ram
  executed-ghost @
  CreateFlag on
  create-forward-warn
  IF ['] reswarn-forward IS resolve-warning THEN
  \ make Alias
  (THeader there 0 T a, H 80 flag! ( S executed-ghost new-ghost )
  \ store  poiter to code-field
  switchram T cfalign H
  there swap T ! H
  there tlastcfa ! 
  dup there resolve 0 ;Resolve !
  >r dup gdoes,
1264
  >end @ >exec @ r> >exec ! ;
anton's avatar
anton committed
1265 1266

: Build:  ( -- [xt] [colon-sys] )
jwilke's avatar
jwilke committed
1267 1268 1269 1270 1271 1272 1273 1274 1275
  :noname postpone TCreate ;

: BuildSmart:  ( -- [xt] [colon-sys] )
  :noname
  [ has-rom [IF] ]
  postpone RTCreate
  [ [ELSE] ]
  postpone TCreate 
  [ [THEN] ] ;
anton's avatar
anton committed
1276 1277

: gdoes>  ( ghost -- addr flag )
jwilke's avatar
jwilke committed
1278
  executed-ghost @
anton's avatar
anton committed
1279
  state @ IF  gexecute true EXIT  THEN
jwilke's avatar
jwilke committed
1280
  >link @ T >body H false ;
anton's avatar
anton committed
1281 1282 1283 1284 1285 1286

\ DO: ;DO                                               11may93jaw
\ changed to ?EXIT                                      10may93jaw

: DO:     ( -- addr [xt] [colon-sys] )
  here ghostheader
1287
  :noname postpone gdoes> postpone ?EXIT ;
anton's avatar
anton committed
1288

1289 1290 1291 1292
: by:     ( -- addr [xt] [colon-sys] ) \ name
  ghost
  :noname postpone gdoes> postpone ?EXIT ;

jwilke's avatar
jwilke committed
1293
: ;DO ( addr [xt] [colon-sys] -- addr )
anton's avatar
anton committed
1294 1295 1296 1297 1298 1299 1300 1301 1302
  postpone ;    ( S addr xt )
  over >exec ! ; immediate

: by      ( -- addr ) \ Name
  ghost >end @ ;

>TARGET
\ Variables and Constants                              05dec92py

jwilke's avatar
jwilke committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
Build:  ( n -- ) ;
by: :docon ( ghost -- n ) T @ H ;DO
Builder (Constant)

Build:  ( n -- ) T , H ;
by (Constant)
Builder Constant

Build:  ( n -- ) T A, H ;
by (Constant)
Builder AConstant

Build:  ( d -- ) T , , H ;
DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
Builder 2Constant

BuildSmart: ;
1320
by: :dovar ( ghost -- addr ) ;DO
anton's avatar
anton committed
1321 1322
Builder Create

jwilke's avatar
jwilke committed
1323 1324 1325 1326 1327
has-rom [IF]
Build: ( n -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
by (Constant)
Builder Variable
[ELSE]
anton's avatar
anton committed
1328 1329 1330
Build: T 0 , H ;
by Create
Builder Variable
jwilke's avatar
jwilke committed
1331
[THEN]
anton's avatar
anton committed
1332

jwilke's avatar
jwilke committed
1333 1334 1335 1336 1337
has-rom [IF]
Build: ( n -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
by (Constant)
Builder AVariable
[ELSE]
anton's avatar
anton committed
1338 1339 1340
Build: T 0 A, H ;
by Create
Builder AVariable
jwilke's avatar
jwilke committed
1341
[THEN]
anton's avatar
anton committed
1342

1343 1344 1345 1346 1347 1348 1349
\ User variables                                       04may94py

>CROSS
Variable tup  0 tup !
Variable tudp 0 tudp !
: u,  ( n -- udp )
  tup @ tudp @ + T  ! H
pazsan's avatar
pazsan committed
1350
  tudp @ dup T cell+ H tudp ! ;
1351 1352
: au, ( n -- udp )
  tup @ tudp @ + T A! H
pazsan's avatar
pazsan committed
1353
  tudp @ dup T cell+ H tudp ! ;
1354 1355 1356
>TARGET

Build: T 0 u, , H ;
1357
by: :douser ( ghost -- up-addr )  T @ H tup @ + ;DO
anton's avatar
anton committed
1358 1359
Builder User

1360 1361
Build: T 0 u, , 0 u, drop H ;
by User
anton's avatar
anton committed
1362 1363
Builder 2User

1364 1365
Build: T 0 au, , H ;
by User
anton's avatar
anton committed
1366 1367
Builder AUser

jwilke's avatar
jwilke committed
1368
BuildSmart: T , H ;
1369
by (Constant)
anton's avatar
anton committed
1370 1371
Builder Value

jwilke's avatar
jwilke committed
1372
BuildSmart: T A, H ;
1373
by (Constant)
pazsan's avatar
pazsan committed
1374 1375
Builder AValue

jwilke's avatar
jwilke committed
1376
BuildSmart:  ( -- ) [T'] noop T A, H ;
1377
by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
anton's avatar
anton committed
1378 1379
Builder Defer

jwilke's avatar
jwilke committed
1380
BuildSmart:  ( inter comp -- ) swap T immediate A, A, H ;
1381
DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
anton's avatar
anton committed
1382
Builder interpret/compile:
1383

pazsan's avatar
pazsan committed
1384 1385 1386 1387 1388 1389 1390 1391
\ Sturctures                                           23feb95py

>CROSS
: nalign ( addr1 n -- addr2 )
\ addr2 is the aligned version of addr1 wrt the alignment size n
 1- tuck +  swap invert and ;
>TARGET

1392 1393 1394 1395
Build: ;
by: :dofield T @ H + ;DO
Builder (Field)

1396 1397 1398
Build: ( align1 offset1 align size "name" --  align2 offset2 )
    rot dup T , H ( align1 align size offset1 )
    + >r nalign r> ;
1399
by (Field)
pazsan's avatar
pazsan committed
1400 1401
Builder Field

1402
: struct  T 1 chars 0 H ;
pazsan's avatar
pazsan committed
1403 1404
: end-struct  T 2Constant H ;

jwilke's avatar
jwilke committed
1405
: cell% ( n -- size align )
1406
    T 1 cells H dup ;
pazsan's avatar
pazsan committed
1407 1408 1409 1410

\ ' 2Constant Alias2 end-struct
\ 0 1 T Chars H 2Constant struct

jwilke's avatar
jwilke committed
1411 1412
0 [IF]

anton's avatar
anton committed
1413 1414 1415 1416 1417
\ structural conditionals                              17dec92py

>CROSS
: ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
: sys?        ( sys -- sys )    dup 0= ?struc ;
jwilke's avatar
jwilke committed
1418 1419 1420 1421 1422
: >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;

: branchoffset ( src dest -- ) - ;
: >resolve    ( sys -- )        T here ( dup ." >" hex. ) over branchoffset swap ! H ;
: <resolve    ( sys -- )        T here ( dup ." <" hex. ) branchoffset , H ;
anton's avatar
anton committed
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
>TARGET

\ Structural Conditionals                              12dec92py

Cond: BUT       restrict? sys? swap ;Cond
Cond: YET       restrict? sys? dup ;Cond

>CROSS
Variable tleavings
>TARGET

Cond: DONE   ( addr -- )  restrict? tleavings @
      BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
      tleavings ! drop ;Cond

>CROSS
: (leave  T here H tleavings @ T , H  tleavings ! ;
>TARGET

Cond: LEAVE     restrict? compile branch (leave ;Cond
Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond

\ Structural Conditionals                              12dec92py

Cond: AHEAD     restrict? compile branch >mark ;Cond
Cond: IF        restrict? compile ?branch >mark ;Cond
jwilke's avatar
jwilke committed
1449
Cond: THEN      restrict? sys? branchto, dup T @ H ?struc >resolve ;Cond
anton's avatar
anton committed
1450 1451
Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond

jwilke's avatar
jwilke committed
1452
Cond: BEGIN     restrict? T branchto, here ( dup ." B" hex. ) H ;Cond
anton's avatar
anton committed
1453 1454 1455 1456 1457
Cond: WHILE     restrict? sys? compile IF swap ;Cond
Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond

pazsan's avatar
pazsan committed
1458
Cond: CASE      restrict? 0 ;Cond
jwilke's avatar
jwilke committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
Cond: OF        restrict? 1+ >r compile over compile =
                compile IF compile drop r> ;Cond
Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond

\ Structural Conditionals                              12dec92py

Cond: DO        restrict? compile (do)   T here H ;Cond
Cond: ?DO       restrict? compile (?do)  T (leave here H ;Cond
Cond: FOR       restrict? compile (for)  T here H ;Cond

>CROSS
: loop]   dup <resolve tcell - compile DONE compile unloop ;
>TARGET

Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond

[ELSE]

\ structural conditionals                              17dec92py

>CROSS
: ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
: sys?        ( sys -- sys )    dup 0= ?struc ;
: >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;

: branchoffset ( src dest -- ) - ;

: >resolve    ( sys -- )        T here ( dup ." >" hex. ) over branchoffset swap ! H ;

: <resolve    ( sys -- )        T here ( dup ." <" hex. ) branchoffset , H ;

:noname compile branch T here branchoffset , H ; IS branch,
:noname compile ?branch T here branchoffset , H ; IS ?branch,
:noname compile branch T here 0 , H ; IS branchmark,
:noname compile ?branch T here 0 , H ; IS  ?branchmark,
:noname dup T @ H ?struc T here over branchoffset swap ! H ; IS branchtoresolve,
:noname branchto, T here H ; IS branchtomark,

>TARGET

\ Structural Conditionals                              12dec92py

Cond: BUT       restrict? sys? swap ;Cond
Cond: YET       restrict? sys? dup ;Cond

>CROSS
Variable tleavings
>TARGET

Cond: DONE   ( addr -- )  restrict? tleavings @
      BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
      tleavings ! drop ;Cond

>CROSS
: (leave  T here H tleavings @ T , H  tleavings ! ;
>TARGET

Cond: LEAVE     restrict? compile branch (leave ;Cond
Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond

\ Structural Conditionals                              12dec92py

Cond: AHEAD     restrict? branchmark, ;Cond
Cond: IF        restrict? ?branchmark, ;Cond
Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond

Cond: BEGIN     restrict? branchtomark, ;Cond
Cond: WHILE     restrict? sys? compile IF swap ;Cond
Cond: AGAIN     restrict? sys? branch, ;Cond
Cond: UNTIL     restrict? sys? ?branch, ;Cond
Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond

Cond: CASE      restrict? 0 ;Cond
Cond: OF        restrict? 1+ >r compile over compile =
                compile IF compile drop r> ;Cond
pazsan's avatar
pazsan committed
1538 1539 1540
Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond

anton's avatar
anton committed
1541 1542 1543
\ Structural Conditionals                              12dec92py

Cond: DO        restrict? compile (do)   T here H ;Cond
anton's avatar
anton committed
1544
Cond: ?DO       restrict? compile (?do)  T (leave here H ;Cond
anton's avatar
anton committed
1545 1546 1547
Cond: FOR       restrict? compile (for)  T here H ;Cond

>CROSS
anton's avatar
anton committed
1548
: loop]   dup <resolve tcell - compile DONE compile unloop ;
anton's avatar
anton committed
1549 1550 1551 1552 1553 1554
>TARGET

Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond

jwilke's avatar
jwilke committed
1555 1556
[THEN]

anton's avatar
anton committed
1557 1558
\ String words                                         23feb93py

jwilke's avatar
jwilke committed
1559
: ,"            [char] " parse T string, align H ;
anton's avatar
anton committed
1560 1561 1562 1563 1564 1565 1566

Cond: ."        restrict? compile (.")     T ," H ;Cond
Cond: S"        restrict? compile (S")     T ," H ;Cond
Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond

Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
: IS            T ' >body ! H ;
pazsan's avatar
pazsan committed
1567 1568
Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
: TO            T ' >body ! H ;
anton's avatar
anton committed
1569

jwilke's avatar
jwilke committed
1570 1571 1572 1573
Cond: defers	T ' >body @ compile, H ;Cond
: on		T -1 swap ! H ; 
: off   	T 0 swap ! H ;

anton's avatar
anton committed
1574 1575 1576 1577
\ LINKED ERR" ENV" 2ENV"                                18may93jaw

\ linked list primitive
: linked        T here over @ A, swap ! H ;
jwilke's avatar
jwilke committed
1578
: chained	T linked A, H ;
anton's avatar
anton committed
1579 1580

: err"   s" ErrLink linked" evaluate T , H
jwilke's avatar
jwilke committed
1581
         [char] " parse T string, align H ;
anton's avatar
anton committed
1582 1583

: env"  [char] " parse s" EnvLink linked" evaluate
jwilke's avatar
jwilke committed
1584
        T string, align , H ;
anton's avatar
anton committed
1585 1586

: 2env" [char] " parse s" EnvLink linked" evaluate
jwilke's avatar
jwilke committed
1587
        here >r T string, align , , H
anton's avatar
anton committed
1588 1589 1590 1591 1592
        r> dup T c@ H 80 and swap T c! H ;

\ compile must be last                                 22feb93py

Cond: compile ( -- ) restrict? \ name
pazsan's avatar
pazsan committed
1593
      bl word gfind dup 0= ABORT" CROSS: Can't compile"
anton's avatar
anton committed
1594 1595 1596 1597 1598 1599
      0> IF    gexecute
         ELSE  dup >magic @ <imm> =
               IF   gexecute
               ELSE compile (compile) gexecute THEN THEN ;Cond

Cond: postpone ( -- ) restrict? \ name
pazsan's avatar
pazsan committed
1600
      bl word gfind dup 0= ABORT" CROSS: Can't compile"
anton's avatar
anton committed
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      0> IF    gexecute
         ELSE  dup >magic @ <imm> =
               IF   gexecute
               ELSE compile (compile) gexecute THEN THEN ;Cond

>MINIMAL
also minimal
\ Usefull words                                        13feb93py

: KB  400 * ;

\ define new [IFDEF] and [IFUNDEF]                      20may93jaw

pazsan's avatar
pazsan committed
1614
: defined? defined? ;
1615 1616
: needed? needed? ;
: doer? doer? ;
anton's avatar
anton committed
1617

jwilke's avatar
jwilke committed
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
\ we want to use IFDEF on compiler directives (e.g. E?) in the source, too

: directive? 
  bl word count [ ' target >wordlist ] aliteral search-wordlist 
  dup IF nip THEN ;

: [IFDEF]  >in @ directive? swap >in !
	   0= IF defined? ELSE name 2drop true THEN
	   postpone [IF] ;

pazsan's avatar
pazsan committed
1628
: [IFUNDEF] defined? 0= postpone [IF] ;
anton's avatar
anton committed
1629 1630 1631

\ C: \- \+ Conditional Compiling                         09jun93jaw

pazsan's avatar
pazsan committed
1632
: C: >in @ defined? 0=
anton's avatar
anton committed
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
     IF    >in ! T : H
     ELSE drop
        BEGIN bl word dup c@
              IF   count comment? s" ;" compare 0= ?EXIT
              ELSE refill 0= ABORT" CROSS: Out of Input while C:"
              THEN
        AGAIN
     THEN ;

also minimal

jwilke's avatar
jwilke committed
1644 1645 1646
\G doesn't skip line when bit is set in debugmask
: \D name evaluate debugmasksource @ and 0= IF postpone \ THEN ;

anton's avatar
anton committed
1647
\G interprets the line if word is not defined
pazsan's avatar
pazsan committed
1648
: \- defined? IF postpone \ THEN ;
anton's avatar
anton committed
1649 1650

\G interprets the line if word is defined
pazsan's avatar
pazsan committed
1651
: \+ defined? 0= IF postpone