cross.fs 55.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
\ Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
anton's avatar
anton committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

\ 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

jwilke's avatar
jwilke committed
22 23 24 25 26 27 28 29 30 31 32
0 
[IF]

ToDo:
Crossdoc destination ./doc/crossdoc.fd makes no sense when
cross.fs is uses seperately. jaw
Do we need this char translation with >address and in branchoffset? 
(>body also affected) jaw
Clean up mark> and >resolve stuff jaw

[THEN]
anton's avatar
anton committed
33 34


jwilke's avatar
jwilke committed
35 36 37 38 39
hex     \ the defualt base for the cross-compiler is hex !!
Warnings off

\ words that are generaly useful

40
: KB  400 * ;
jwilke's avatar
jwilke committed
41 42 43 44 45
: >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
46

pazsan's avatar
pazsan committed
47 48 49
: string, ( c-addr u -- )
    \ puts down string as cstring
    dup c, here swap chars dup allot move ;
50

jwilke's avatar
jwilke committed
51
: SetValue ( n -- <name> )
pazsan's avatar
pazsan committed
52 53
\G Same behaviour as "Value" if the <name> is not defined
\G Same behaviour as "to" if <name> is defined
jwilke's avatar
jwilke committed
54
\G SetValue searches in the current vocabulary
jwilke's avatar
jwilke committed
55 56 57 58 59 60
  save-input bl word >r restore-input throw r> count
  get-current search-wordlist
  IF	drop >r
	\ we have to set current to be topmost context wordlist
	get-order get-order get-current swap 1+ set-order
	r> ['] to execute
jwilke's avatar
jwilke committed
61
	set-order
jwilke's avatar
jwilke committed
62
  ELSE Value THEN ;
jwilke's avatar
jwilke committed
63 64

: DefaultValue ( n -- <name> )
pazsan's avatar
pazsan committed
65 66
\G Same behaviour as "Value" if the <name> is not defined
\G DefaultValue searches in the current vocabulary
jwilke's avatar
jwilke committed
67 68
 save-input bl word >r restore-input throw r> count
 get-current search-wordlist
69
 IF bl word drop 2drop ELSE Value THEN ;
anton's avatar
anton committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

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
97 98 99
\ 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
100

jwilke's avatar
jwilke committed
101 102 103 104 105
: 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
106

jwilke's avatar
jwilke committed
107
\ Begin CROSS COMPILER:
anton's avatar
anton committed
108

pazsan's avatar
pazsan committed
109 110


jwilke's avatar
jwilke committed
111 112 113 114 115 116 117 118 119 120
\ \ --------------------        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

jwilke's avatar
jwilke committed
121 122 123


  
jwilke's avatar
jwilke committed
124 125 126

previous >CROSS

jwilke's avatar
jwilke committed
127 128 129
: .dec
  base @ decimal swap . base ! ;

jwilke's avatar
jwilke committed
130 131
: .sourcepos
  cr sourcefilename type ." :"
jwilke's avatar
jwilke committed
132
  sourceline# .dec ;
jwilke's avatar
jwilke committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146

: 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]

jwilke's avatar
jwilke committed
147
\ debugging
jwilke's avatar
jwilke committed
148

jwilke's avatar
jwilke committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
0 [IF]

This implements debugflags for the cross compiler and the compiled
images. It works identical to the has-flags in the environment.
The debugflags are defined in a vocabluary. If the word exists and
its value is true, the flag is switched on.

[THEN]

Vocabulary debugflags	\ debug flags for cross
also debugflags get-order over
Constant debugflags-wl
set-order previous

: DebugFlag
  get-current >r debugflags-wl set-current
  SetValue
  r> set-current ;

: Debug? ( adr u -- flag )
\G return true if debug flag is defined or switched on
  debugflags-wl search-wordlist
  IF EXECUTE
  ELSE false THEN ;

: D? ( <name> -- flag )
\G return true if debug flag is defined or switched on
\G while compiling we do not return the current value but
  bl word count debug? ;

: [d?]
\G compile the value-xt so the debug flag can be switched
\G the flag must exist!
  bl word count debugflags-wl search-wordlist
  IF 	compile,
  ELSE  -1 ABORT" unknown debug flag"
	\ POSTPONE false 
  THEN ; immediate
pazsan's avatar
pazsan committed
187

jwilke's avatar
jwilke committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
\ \ 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
211
hex
jwilke's avatar
jwilke committed
212 213
4711 Constant <fwd>             4712 Constant <res>
4713 Constant <imm>             4714 Constant <do:>
214
4715 Constant <skip>
jwilke's avatar
jwilke committed
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

\ 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
pazsan's avatar
pazsan committed
274 275 276
  ELSE  2drop 
	\ true abort" CROSS: Ghostnames inconsistent"
	s" ?!?!?!"
jwilke's avatar
jwilke committed
277 278 279 280 281 282 283
  THEN ;

' >ghostname ALIAS @name

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

284 285 286
: undefined? ( ghost -- flag )
  >magic @ dup <fwd> = swap <skip> = or ;

jwilke's avatar
jwilke committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300
\ 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
pazsan's avatar
pazsan committed
301
ghost :dovar					drop
jwilke's avatar
jwilke committed
302 303
ghost over      ghost =         ghost drop      2drop drop
ghost - drop
pazsan's avatar
pazsan committed
304 305
ghost 2drop drop
ghost 2dup drop
jwilke's avatar
jwilke committed
306 307 308 309 310 311 312 313 314 315 316 317 318

\ \ 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

jwilke's avatar
jwilke committed
319
: environment? ( adr len -- [ x ] true | false )
jwilke's avatar
jwilke committed
320 321 322
  target-environment search-wordlist 
  IF execute true ELSE false THEN ;

jwilke's avatar
jwilke committed
323
: e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
jwilke's avatar
jwilke committed
324

jwilke's avatar
jwilke committed
325
: has? 	bl word count T environment? H 
jwilke's avatar
jwilke committed
326 327
	IF 	\ environment variable is present, return its value
	ELSE	\ environment variable is not present, return false
328
		false \ debug true ABORT" arg" 
jwilke's avatar
jwilke committed
329
	THEN ;
jwilke's avatar
jwilke committed
330 331 332

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

pazsan's avatar
pazsan committed
333 334
>ENVIRON get-order get-current swap 1+ set-order
true SetValue compiler
jwilke's avatar
jwilke committed
335
true  SetValue cross
pazsan's avatar
pazsan committed
336 337
true SetValue standard-threading
>TARGET previous
jwilke's avatar
jwilke committed
338

339

jwilke's avatar
jwilke committed
340 341
mach-file count included hex

jwilke's avatar
jwilke committed
342 343
>ENVIRON

pazsan's avatar
pazsan committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
T has? ec H
[IF]
false DefaultValue relocate
false DefaultValue file
false DefaultValue OS
false DefaultValue prims
false DefaultValue floating
false DefaultValue glocals
false DefaultValue dcomps
false DefaultValue hash
false DefaultValue xconds
false DefaultValue header
[THEN]

true DefaultValue interpreter
true DefaultValue ITC
false DefaultValue rom
jwilke's avatar
jwilke committed
361
true DefaultValue standardthreading
jwilke's avatar
jwilke committed
362

jwilke's avatar
jwilke committed
363 364 365 366 367
>TARGET
s" relocate" T environment? H 
[IF]	SetValue NIL
[ELSE]	>ENVIRON T NIL H SetValue relocate
[THEN]
pazsan's avatar
pazsan committed
368 369 370

>CROSS

jwilke's avatar
jwilke committed
371
\ \ Create additional parameters                         19jan95py
pazsan's avatar
pazsan committed
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
\ currently cross only works for host machines with address-unit-bits
\ eual to 8 because of s! and sc!
\ but I start to query the environment just to modularize a little bit

: check-address-unit-bits ( -- )	
\	s" ADDRESS-UNIT-BITS" environment?
\	IF 8 <> ELSE true THEN
\	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"

\	shit, this doesn't work because environment? is only defined for 
\	gforth.fi and not kernl???.fi
	;

check-address-unit-bits
8 Constant bits/byte	\ we define: byte is address-unit

1 bits/byte lshift Constant maxbyte 
jwilke's avatar
jwilke committed
390 391
\ this sets byte size for the target machine, an (probably right guess) jaw

pazsan's avatar
pazsan committed
392
T
393 394 395 396 397 398 399 400 401 402 403 404 405 406
NIL		   	Constant TNIL
cell               	Constant tcell
cell<<             	Constant tcell<<
cell>bit           	Constant tcell>bit
bits/char          	Constant tbits/char
bits/char H bits/byte T /      
			Constant tchar
float             	Constant tfloat
1 bits/char lshift 	Constant tmaxchar
[IFUNDEF] bits/byte
8			Constant tbits/byte
[ELSE]
bits/byte		Constant tbits/byte
[THEN]
pazsan's avatar
pazsan committed
407
H
408 409
tbits/byte bits/byte /	Constant tbyte

pazsan's avatar
pazsan committed
410

anton's avatar
anton committed
411 412 413 414 415 416 417 418
\ 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
419 420 421 422 423
\ statistics						10jun97jaw

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

jwilke's avatar
jwilke committed
424 425 426 427 428 429
: target>bitmask-size ( u1 -- u2 )
  1- tcell>bit rshift 1+ ;

: allocatetarget ( size --- adr )
  dup allocate ABORT" CROSS: No memory for target"
  swap over swap erase ;
jwilke's avatar
jwilke committed
430

pazsan's avatar
pazsan committed
431
\ \ memregion.fs
jwilke's avatar
jwilke committed
432 433 434 435 436 437 438 439


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 !


jwilke's avatar
jwilke committed
440 441 442 443
: >rname 6 cells + ;
: >rbm   5 cells + ;
: >rmem  4 cells + ;
: >rlink 3 cells + ;
jwilke's avatar
jwilke committed
444 445 446 447 448 449 450 451 452 453 454 455 456
: >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 ) ,
jwilke's avatar
jwilke committed
457
	region-link linked 0 , 0 , bl word count string,
jwilke's avatar
jwilke committed
458 459 460
  ELSE	\ store new parameters in region
        bl word drop
	>body >r r@ last-defined-region !
jwilke's avatar
jwilke committed
461
	r@ >rlen ! dup r@ >rstart ! r> >rdp !
jwilke's avatar
jwilke committed
462 463 464
  THEN ;

: borders ( region -- startaddr endaddr ) \G returns lower and upper region border
jwilke's avatar
jwilke committed
465
  dup >rstart @ swap >rlen @ over + ;
jwilke's avatar
jwilke committed
466 467

: extent  ( region -- startaddr len )   \G returns the really used area
jwilke's avatar
jwilke committed
468
  dup >rstart @ swap >rdp @ over - ;
jwilke's avatar
jwilke committed
469 470

: area ( region -- startaddr totallen ) \G returns the total area
jwilke's avatar
jwilke committed
471
  dup >rstart swap >rlen @ ;
jwilke's avatar
jwilke committed
472 473 474

: mirrored                              \G mark a region as mirrored
  mirrored-link
jwilke's avatar
jwilke committed
475
  align linked last-defined-region @ , ;
jwilke's avatar
jwilke committed
476

jwilke's avatar
jwilke committed
477 478
: .addr ( u -- )
\G prints a 16 or 32 Bit nice hex value
jwilke's avatar
jwilke committed
479 480 481 482 483 484 485 486 487 488 489 490 491
  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
jwilke's avatar
jwilke committed
492 493 494
  WHILE cr
        0 >rlink - >r
        r@ >rname count tuck type
jwilke's avatar
jwilke committed
495
        12 swap - 0 max spaces space
jwilke's avatar
jwilke committed
496 497 498
        ." Start: " r@ >rstart @ dup .addr space
        ." End: " r@ >rlen @ + .addr space
        ." DP: " r> >rdp @ .addr
jwilke's avatar
jwilke committed
499
  REPEAT drop
jwilke's avatar
jwilke committed
500
  s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
501 502 503
  cr ." Mirrored:"
  mirrored-link @
  BEGIN dup
jwilke's avatar
jwilke committed
504
  WHILE space dup cell+ @ >rname count type @
jwilke's avatar
jwilke committed
505 506 507 508 509 510 511 512 513 514 515
  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

jwilke's avatar
jwilke committed
516
T has? rom H
jwilke's avatar
jwilke committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
[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
jwilke's avatar
jwilke committed
534
  s" rom" T $has? H
jwilke's avatar
jwilke committed
535
  IF  \ check for ram and rom...
536
      \ address-space area nip 0<>
jwilke's avatar
jwilke committed
537 538
      ram-dictionary area nip 0<>
      rom-dictionary area nip 0<>
539
      and 0=
jwilke's avatar
jwilke committed
540 541 542 543 544 545 546 547
      ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
  THEN
  address-space area nip
  IF
      address-space area
  ELSE
      dictionary area
  THEN
jwilke's avatar
jwilke committed
548
  nip 0=
jwilke's avatar
jwilke committed
549
  ABORT" CROSS: define at least address-space or dictionary!!"
jwilke's avatar
jwilke committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563

  \ allocate target for each region
  region-link
  BEGIN @ dup
  WHILE dup
        0 >rlink - >r
        r@ >rlen @
        IF      \ allocate mem
                r@ >rlen @ dup

                allocatetarget dup image !
                r@ >rmem !

                target>bitmask-size allocatetarget
564
                dup bit$ !
jwilke's avatar
jwilke committed
565 566 567
                r> >rbm !

        ELSE    r> drop THEN
568 569 570 571 572 573 574 575 576 577
   REPEAT drop ;

\ MakeKernal                                           		22feb99jaw

: makekernel ( targetsize -- targetsize )
  dup dictionary >rlen ! setup-target ;

>MINIMAL
: makekernel makekernel ;
>CROSS
jwilke's avatar
jwilke committed
578

pazsan's avatar
pazsan committed
579
\ \ switched tdp for rom support				03jun97jaw
jwilke's avatar
jwilke committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

\ 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

jwilke's avatar
jwilke committed
613 614 615 616
: activate ( region -- )
\G next code goes to this region
  >rdp to tdp ;

jwilke's avatar
jwilke committed
617
: (switchram)
jwilke's avatar
jwilke committed
618
  fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
619
  ram-dictionary activate ;
jwilke's avatar
jwilke committed
620 621 622 623 624 625

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

: switchrom
jwilke's avatar
jwilke committed
626
  fixed @ ?EXIT rom-dictionary activate ;
jwilke's avatar
jwilke committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

: >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 ! ;

jwilke's avatar
jwilke committed
642
\ default compilation goes to rom
jwilke's avatar
jwilke committed
643 644 645 646
\ when romable support is off, only the rom switch is used (!!)
>auto

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

anton's avatar
anton committed
648 649
>TARGET

jwilke's avatar
jwilke committed
650 651
\ \ Target Memory Handling

anton's avatar
anton committed
652 653
\ Byte ordering and cell size                          06oct92py

pazsan's avatar
pazsan committed
654 655
: cell+         tcell + ;
: cells         tcell<< lshift ;
656 657
: chars         tchar * ;
: char+		tchar + ;
pazsan's avatar
pazsan committed
658
: floats	tfloat * ;
659
    
anton's avatar
anton committed
660
>CROSS
pazsan's avatar
pazsan committed
661
: cell/         tcell<< rshift ;
anton's avatar
anton committed
662 663
>TARGET
20 CONSTANT bl
jwilke's avatar
jwilke committed
664
\ TNIL Constant NIL
anton's avatar
anton committed
665 666 667

>CROSS

668 669
bigendian
[IF]
jwilke's avatar
jwilke committed
670
   : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
671
     DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
jwilke's avatar
jwilke committed
672
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds
673
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
674 675 676 677
   : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
     DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
   : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
pazsan's avatar
pazsan committed
678
[ELSE]
jwilke's avatar
jwilke committed
679
   : S!  ( n addr -- )  >r s>d r> tcell bounds
680
     DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
jwilke's avatar
jwilke committed
681
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
682
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
683 684 685 686
   : Sc!  ( n addr -- )  >r s>d r> tchar bounds
     DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
   : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
anton's avatar
anton committed
687 688
[THEN]

jwilke's avatar
jwilke committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
: taddr>region ( taddr -- region | 0 )
\G finds for a target-address the correct region
\G returns 0 if taddr is not in range of a target memory region
  region-link
  BEGIN @ dup
  WHILE dup >r
        0 >rlink - >r
        r@ >rlen @
        IF      dup r@ borders within
                IF r> r> drop nip EXIT THEN
        THEN
        r> drop
        r>
  REPEAT
  2drop 0 ;

: (>regionimage) ( taddr -- 'taddr )
  dup
  \ find region we want to address
  taddr>region dup 0= ABORT" Address out of range!"
  >r
  \ calculate offset in region
  r@ >rstart @ -
  \ add regions real address in our memory
  r> >rmem @ + ;

anton's avatar
anton committed
715 716 717 718 719 720 721
\ 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
722
: -bit ( addr n -- )  >bit invert over c@ and swap c! ;
jwilke's avatar
jwilke committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

: (relon) ( taddr -- )  bit$ @ swap cell/ +bit ;
: (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;

: (>image) ( taddr -- absaddr ) image @ + ;

DEFER >image
DEFER relon
DEFER reloff
DEFER correcter

T has? relocate H
[IF]
' (relon) IS relon
' (reloff) IS reloff
' (>image) IS >image
[ELSE]
' drop IS relon
' drop IS reloff
jwilke's avatar
jwilke committed
742
' (>regionimage) IS >image
jwilke's avatar
jwilke committed
743
[THEN]
anton's avatar
anton committed
744 745 746 747

\ Target memory access                                 06oct92py

: align+  ( taddr -- rest )
anton's avatar
anton committed
748
    tcell tuck 1- and - [ tcell 1- ] Literal and ;
749
: cfalign+  ( taddr -- rest )
pazsan's avatar
pazsan committed
750
    \ see kernel.fs:cfaligned
pazsan's avatar
pazsan committed
751
    /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
anton's avatar
anton committed
752 753 754 755 756

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

757
: cfaligned ( taddr1 -- taddr2 )
pazsan's avatar
pazsan committed
758
    \ see kernel.fs
759 760
    dup cfalign+ + ;

jwilke's avatar
jwilke committed
761 762
: @  ( taddr -- w )     >image S@ ;
: !  ( w taddr -- )     >image S! ;
763 764
: c@ ( taddr -- char )  >image Sc@ ;
: c! ( char taddr -- )  >image Sc! ;
765 766
: 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
: 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
anton's avatar
anton committed
767 768 769 770 771 772

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

: here  ( -- there )    there ;
: allot ( n -- )        tdp +! ;
jwilke's avatar
jwilke committed
773
: ,     ( w -- )        T here H tcell T allot  ! H T here drop H ;
774
: c,    ( char -- )     T here    tchar allot c! H ;
jwilke's avatar
jwilke committed
775
: align ( -- )          T here H align+ 0 ?DO  bl T c, tchar H +LOOP ;
776
: cfalign ( -- )
777
    T here H cfalign+ 0 ?DO  bl T c, tchar H +LOOP ;
anton's avatar
anton committed
778

779
: >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
780 781
: A!                    swap >address swap dup relon T ! H ;
: A,    ( w -- )        >address T here H relon T , H ;
anton's avatar
anton committed
782 783 784

>CROSS

jwilke's avatar
jwilke committed
785 786
: tcmove ( source dest len -- )
\G cmove in target memory
787
  tchar * bounds
jwilke's avatar
jwilke committed
788
  ?DO  dup T c@ H I T c! H 1+
789
  tchar +LOOP  drop ;
anton's avatar
anton committed
790

jwilke's avatar
jwilke committed
791 792
\ \ Load Assembler

anton's avatar
anton committed
793
>TARGET
jwilke's avatar
jwilke committed
794
H also Forth definitions
anton's avatar
anton committed
795

jwilke's avatar
jwilke committed
796 797 798 799 800 801
: 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
802

jwilke's avatar
jwilke committed
803
[IFDEF] asm-include asm-include [THEN] hex
anton's avatar
anton committed
804

jwilke's avatar
jwilke committed
805 806 807 808 809
previous
>CROSS H

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

pazsan's avatar
pazsan committed
810 811 812 813 814 815 816 817
\  Compiler States

Variable comp-state
0 Constant interpreting
1 Constant compiling
2 Constant resolving
3 Constant assembling

jwilke's avatar
jwilke committed
818 819
Defer lit, ( n -- )
Defer alit, ( n -- )
pazsan's avatar
pazsan committed
820 821 822 823 824 825 826 827 828 829 830

Defer branch, ( target-addr -- )	\ compiles a branch
Defer ?branch, ( target-addr -- )	\ compiles a ?branch
Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
Defer branchfrom, ( -- )		\ ?!
Defer branchtomark, ( -- target-addr )	\ marks a branch destination

jwilke's avatar
jwilke committed
831
Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
pazsan's avatar
pazsan committed
832
Defer colonmark, ( -- addr )		\ marks a colon call
jwilke's avatar
jwilke committed
833
Defer colon-resolve ( tcfa addr -- )
pazsan's avatar
pazsan committed
834

jwilke's avatar
jwilke committed
835
Defer addr-resolve ( target-addr addr -- )
pazsan's avatar
pazsan committed
836 837 838 839 840 841 842 843
Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )

Defer do,	( -- do-token )
Defer ?do,	( -- ?do-token )
Defer for,	( -- for-token )
Defer loop,	( do-token / ?do-token -- )
Defer +loop,	( do-token / ?do-token -- )
Defer next,	( for-token )
anton's avatar
anton committed
844

jwilke's avatar
jwilke committed
845 846 847
[IFUNDEF] ca>native
defer ca>native	
[THEN]
anton's avatar
anton committed
848

jwilke's avatar
jwilke committed
849 850 851 852 853
>TARGET
DEFER >body             \ we need the system >body
			\ and the target >body
>CROSS
T 2 cells H VALUE xt>body
pazsan's avatar
pazsan committed
854 855
DEFER doprim,	\ compiles start of a primitive
DEFER docol,   	\ compiles start of a colon definition
jwilke's avatar
jwilke committed
856 857 858 859
DEFER doer,		
DEFER fini,      \ compiles end of definition ;s
DEFER doeshandler,
DEFER dodoes,
anton's avatar
anton committed
860

jwilke's avatar
jwilke committed
861 862
DEFER ]comp     \ starts compilation
DEFER comp[     \ ends compilation
anton's avatar
anton committed
863

pazsan's avatar
pazsan committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
: (cc) T a, H ;					' (cc) IS colon,

: (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
: (ar) T ! H ;					' (ar) IS addr-resolve
: (dr)  ( ghost res-pnt target-addr addr )
	>tempdp drop over 
	dup >magic @ <do:> =
	IF 	doer,
	ELSE	dodoes,
	THEN 
	tempdp> ;				' (dr) IS doer-resolve

: (cm) ( -- addr )
    T here align H
    -1 colon, ;					' (cm) IS colonmark,
anton's avatar
anton committed
879

jwilke's avatar
jwilke committed
880 881 882
>TARGET
: compile, colon, ;
>CROSS
anton's avatar
anton committed
883

jwilke's avatar
jwilke committed
884 885
\ file loading

pazsan's avatar
pazsan committed
886 887 888
: >fl-id   1 cells + ;
: >fl-name 2 cells + ;

jwilke's avatar
jwilke committed
889
Variable filelist 0 filelist !
jwilke's avatar
jwilke committed
890
Create NoFile ," #load-file#"
pazsan's avatar
pazsan committed
891
0 Value	 filemem
jwilke's avatar
jwilke committed
892
: loadfile  FileMem ?dup IF >fl-name ELSE NoFile THEN ;
jwilke's avatar
jwilke committed
893

pazsan's avatar
pazsan committed
894
1 [IF] \ !! JAW WIP
anton's avatar
anton committed
895

jwilke's avatar
jwilke committed
896
: add-included-file ( adr len -- )
pazsan's avatar
pazsan committed
897 898
	dup char+ >fl-name allocate throw >r
	r@ >fl-name place
jwilke's avatar
jwilke committed
899
	filelist @ r@ !
pazsan's avatar
pazsan committed
900 901
	r> dup filelist ! to FileMem
	;
jwilke's avatar
jwilke committed
902 903 904 905 906 907 908 909 910 911

: included? ( c-addr u -- f )
  	filelist
	BEGIN	@ dup
	WHILE	>r r@ 1 cells + count compare 0=
		IF rdrop 2drop true EXIT THEN
		r>
	REPEAT
	2drop drop false ;	

jwilke's avatar
jwilke committed
912 913
false DebugFlag showincludedfiles

jwilke's avatar
jwilke committed
914
: included 
jwilke's avatar
jwilke committed
915 916
	[d?] showincludedfiles
	IF	cr ." Including: " 2dup type ." ..." THEN
pazsan's avatar
pazsan committed
917 918 919
	FileMem >r
	2dup add-included-file included 
	r> to FileMem ;
jwilke's avatar
jwilke committed
920 921 922 923 924 925

: include bl word count included ;

: require bl word count included ;

[THEN]
anton's avatar
anton committed
926

jwilke's avatar
jwilke committed
927
\ resolve structure
anton's avatar
anton committed
928

jwilke's avatar
jwilke committed
929
: >next ;		\ link to next field
pazsan's avatar
pazsan committed
930
: >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
jwilke's avatar
jwilke committed
931
: >taddr cell+ cell+ ;	
jwilke's avatar
jwilke committed
932
: >ghost 3 cells + ;
jwilke's avatar
jwilke committed
933 934
: >file 4 cells + ;
: >line 5 cells + ;
anton's avatar
anton committed
935

pazsan's avatar
pazsan committed
936 937 938 939 940 941 942 943 944 945
: (refered) ( ghost addr tag -- )
\G creates a reference to ghost at address taddr
    rot >r here r@ >link @ , r> >link ! 
    ( taddr tag ) ,
    ( taddr ) , 
    last-header-ghost @ , 
    loadfile , 
    sourceline# , 
  ;

jwilke's avatar
jwilke committed
946
: refered ( ghost tag -- )
jwilke's avatar
jwilke committed
947
\G creates a resolve structure
pazsan's avatar
pazsan committed
948 949 950 951 952 953 954 955 956 957 958 959 960
    T here aligned H swap (refered)
  ;

: killref ( addr ghost -- )
\G kills a forward reference to ghost at position addr
\G this is used to eleminate a :dovar refence after making a DOES>
    dup >magic @ <fwd> <> IF 2drop EXIT THEN
    swap >r >link
    BEGIN dup @ dup  ( addr last this )
    WHILE dup >taddr @ r@ =
 	 IF   @ over !
	 ELSE nip THEN
    REPEAT rdrop 2drop 
jwilke's avatar
jwilke committed
961
  ;
anton's avatar
anton committed
962

jwilke's avatar
jwilke committed
963
Defer resolve-warning
anton's avatar
anton committed
964

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

jwilke's avatar
jwilke committed
968 969 970
: reswarn-forward ( ghost res-struct -- ghost res-struct )
  over warnhead >ghostname type dup ."  is referenced in " 
  >ghost @ >ghostname type ;
anton's avatar
anton committed
971

jwilke's avatar
jwilke committed
972 973
\ ' reswarn-test IS resolve-warning
 
anton's avatar
anton committed
974 975
\ resolve                                              14oct92py

pazsan's avatar
pazsan committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989
 : resolve-loop ( ghost resolve-list tcfa -- )
    >r
    BEGIN dup WHILE 
\  	  dup >tag @ 2 = IF reswarn-forward THEN
	  resolve-warning 
	  r@ over >taddr @ 
	  2 pick >tag @
	  CASE	0 OF colon-resolve ENDOF
		1 OF addr-resolve ENDOF
		2 OF doer-resolve ENDOF
	  ENDCASE
	  @ \ next list element
    REPEAT 2drop rdrop 
  ;
jwilke's avatar
jwilke committed
990 991 992 993

\ : 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
994 995 996

\ exists                                                9may93jaw

jwilke's avatar
jwilke committed
997 998 999 1000 1001
Variable TWarnings
TWarnings on
Variable Exists-Warnings
Exists-Warnings on

anton's avatar
anton committed
1002 1003 1004 1005 1006
: exists ( ghost tcfa -- )
  over GhostNames
  BEGIN @ dup
  WHILE 2dup cell+ @ =
  UNTIL
jwilke's avatar
jwilke committed
1007 1008 1009 1010 1011
        2 cells + count
        TWarnings @ Exists-Warnings @ and
        IF warnhead type ."  exists"
        ELSE 2drop THEN
        drop swap >link !
pazsan's avatar
pazsan committed
1012
  ELSE  true abort" CROSS: Ghostnames inconsistent "
anton's avatar
anton committed
1013 1014 1015
  THEN ;

: resolve  ( ghost tcfa -- )
pazsan's avatar
pazsan committed
1016 1017 1018
\G resolve referencies to ghost with tcfa
    \ is ghost resolved?, second resolve means another definition with the
    \ same name
1019
    over undefined? 0= IF  exists EXIT THEN
pazsan's avatar
pazsan committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    \ get linked-list
    swap >r r@ >link @ swap \ ( list tcfa R: ghost )
    \ mark ghost as resolved
    dup r@ >link ! <res> r@ >magic !
    \ loop through forward referencies
    r> -rot 
    comp-state @ >r Resolving comp-state !
    resolve-loop 
    r> comp-state !

    ['] noop IS resolve-warning 
jwilke's avatar
jwilke committed
1031
  ;
anton's avatar
anton committed
1032 1033 1034

\ gexecute ghost,                                      01nov92py

jwilke's avatar
jwilke committed
1035
: is-forward   ( ghost -- )
pazsan's avatar
pazsan committed
1036
  colonmark, 0 (refered) ; \ compile space for call
jwilke's avatar
jwilke committed
1037 1038 1039 1040 1041 1042 1043 1044 1045

: 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
1046

jwilke's avatar
jwilke committed
1047
\ !! : ghost,     ghost  gexecute ;
anton's avatar
anton committed
1048 1049 1050 1051 1052 1053 1054

\ .unresolved                                          11may93jaw

variable ResolveFlag

\ ?touched                                             11may93jaw

jwilke's avatar
jwilke committed
1055
: ?touched ( ghost -- flag ) dup forward? swap >link @
anton's avatar
anton committed
1056 1057
                               0 <> and ;

jwilke's avatar
jwilke committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
: .forwarddefs ( ghost -- )
	."  appeared in:"
	>link
	BEGIN	@ dup
	WHILE	cr 5 spaces
		dup >ghost @ >ghostname type
		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
		."  line " dup >line @ .dec
	REPEAT 
	drop ;

anton's avatar
anton committed
1069 1070
: ?resolved  ( ghostname -- )
  dup cell+ @ ?touched
jwilke's avatar
jwilke committed
1071 1072 1073 1074 1075
  IF  	dup 
	cell+ cell+ count cr type ResolveFlag on 
	cell+ @ .forwarddefs
  ELSE 	drop 
  THEN ;
anton's avatar
anton committed
1076 1077 1078 1079 1080 1081 1082

>MINIMAL
: .unresolved  ( -- )
  ResolveFlag off cr ." Unresolved: "
  Ghostnames
  BEGIN @ dup
  WHILE dup ?resolved
anton's avatar
anton committed
1083 1084
  REPEAT drop ResolveFlag @
  IF
anton's avatar
anton committed
1085
      -1 abort" Unresolved words!"
anton's avatar
anton committed
1086 1087 1088 1089
  ELSE
      ." Nothing!"
  THEN
  cr ;
anton's avatar
anton committed
1090

jwilke's avatar
jwilke committed
1091 1092 1093 1094 1095
: .stats
  base @ >r decimal
  cr ." named Headers: " headers-named @ . 
  r> base ! ;

anton's avatar
anton committed
1096 1097 1098 1099 1100 1101 1102 1103
>CROSS
\ Header states                                        12dec92py

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

VARIABLE ^imm

>TARGET
1104
: immediate     40 flag!
1105
                ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
anton's avatar
anton committed
1106 1107
                <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                <imm> ^imm @ ! ;
1108
: restrict      20 flag! ;
jwilke's avatar
jwilke committed
1109

pazsan's avatar
pazsan committed
1110 1111 1112 1113
: isdoer	
\G define a forth word as doer, this makes obviously only sence on
\G forth processors such as the PSC1000
		<do:> last-header-ghost @ >magic ! ;
anton's avatar
anton committed
1114 1115 1116 1117 1118 1119
>CROSS

\ ALIAS2 ansforth conform alias                          9may93jaw

: ALIAS2 create here 0 , DOES> @ execute ;
\ usage:
1120
\ ' <name> alias2 bla !
anton's avatar
anton committed
1121 1122 1123

\ Target Header Creation                               01nov92py

jwilke's avatar
jwilke committed
1124
>TARGET
anton's avatar
anton committed
1125
: string,  ( addr count -- )
pazsan's avatar
pazsan committed
1126
  dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
jwilke's avatar
jwilke committed
1127
: name,  ( "name" -- )  bl word count T string, cfalign H ;
anton's avatar
anton committed
1128
: view,   ( -- ) ( dummy ) ;
jwilke's avatar
jwilke committed
1129
>CROSS
anton's avatar
anton committed
1130

1131 1132
\ Target Document Creation (goes to crossdoc.fd)       05jul95py

pazsan's avatar
pazsan committed
1133
s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
1134 1135
\ contains the file-id of the documentation file

pazsan's avatar
pazsan committed
1136
: T-\G ( -- )
1137
    source >in @ /string doc-file-id write-line throw
pazsan's avatar
pazsan committed
1138
    postpone \ ;
1139

pazsan's avatar
pazsan committed
1140
Variable to-doc  to-doc on
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

: 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
1153
	T-\G
1154
	>in !
pazsan's avatar
pazsan committed
1155
    THEN ;
1156

pazsan's avatar
pazsan committed
1157 1158
\ Target TAGS creation

pazsan's avatar
pazsan committed
1159
s" kernel.TAGS" r/w create-file throw value tag-file-id
pazsan's avatar
pazsan committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
\ 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
1172
	sourcefilename 2dup
pazsan's avatar
pazsan committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	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
1186
	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
pazsan's avatar
pazsan committed
1187 1188 1189 1190 1191
\	>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
1192 1193 1194 1195
\ Check for words

Defer skip? ' false IS skip?

1196 1197 1198 1199 1200 1201
: skipdef ( <name> -- )
\G skip definition of an undefined word in undef-words mode
    ghost dup forward?
    IF  >magic <skip> swap !
    ELSE drop THEN ;

pazsan's avatar
pazsan committed
1202
: defined? ( -- flag ) \ name
1203 1204 1205 1206 1207
    ghost undefined? 0= ;

: defined2? ( -- flag ) \ name
\G return true for anything else than forward, even for <skip>
\G that's what we want
jwilke's avatar
jwilke committed
1208
    ghost forward? 0= ;
pazsan's avatar
pazsan committed
1209 1210

: needed? ( -- flag ) \ name
anton's avatar
anton committed
1211 1212 1213 1214 1215
\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
1216
    IF dup undefined?
anton's avatar
anton committed
1217 1218 1219
	nip
	0=
    ELSE  drop true  THEN ;
pazsan's avatar
pazsan committed
1220

1221 1222 1223
: doer? ( -- flag ) \ name
    ghost >magic @ <do:> = ;

pazsan's avatar
pazsan committed
1224 1225 1226
: skip-defs ( -- )
    BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;

pazsan's avatar
pazsan committed
1227 1228
\ Target header creation

pazsan's avatar
pazsan committed
1229 1230
Variable CreateFlag
CreateFlag off
jwilke's avatar
jwilke committed
1231

pazsan's avatar
pazsan committed
1232 1233
Variable NoHeaderFlag
NoHeaderFlag off
anton's avatar
anton committed
1234

pazsan's avatar
pazsan committed
1235 1236 1237 1238
: 0.r ( n1 n2 -- ) 
    base @ >r hex 
    0 swap <# 0 ?DO # LOOP #> type 
    r> base ! ;
jwilke's avatar
jwilke committed
1239 1240 1241 1242 1243 1244 1245
: .sym
  bounds 
  DO I c@ dup
	CASE	'/ OF drop ." \/" ENDOF
		'\ OF drop ." \\" ENDOF
		dup OF emit ENDOF
	ENDCASE
pazsan's avatar
pazsan committed
1246
    LOOP ;
jwilke's avatar
jwilke committed
1247

pazsan's avatar
pazsan committed
1248
: (Theader ( "name" -- ghost )
pazsan's avatar
pazsan committed
1249 1250 1251 1252 1253 1254 1255 1256
    \  >in @ bl word count type 2 spaces >in !
    \ wordheaders will always be compiled to rom
    switchrom
    \ build header in target
    NoHeaderFlag @
    IF  NoHeaderFlag off
    ELSE
	T align H view,
1257
	tlast @ dup 0> IF  T 1 cells - H THEN T A, H  there tlast !
pazsan's avatar
pazsan committed
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	1 headers-named +!	\ Statistic
	>in @ T name, H >in !
    THEN
    T cfalign here H tlastcfa !
    \ Symbol table
\    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
    CreateFlag @
    IF
	>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
    ELSE ghost
    THEN
    dup Last-Header-Ghost !
    dup >magic ^imm !     \ a pointer for immediate
    Already @
    IF  dup >end tdoes !
    ELSE 0 tdoes !
    THEN
    80 flag!
    cross-doc-entry cross-tag-entry ;
anton's avatar
anton committed
1280 1281

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

1285 1286
: Theader  ( "name" -- ghost )
  (THeader dup there resolve 0 ;Resolve ! ;
anton's avatar
anton committed
1287 1288 1289

>TARGET
: Alias    ( cfa -- ) \ name
pazsan's avatar
pazsan committed
1290
    >in @ skip? IF  2drop  EXIT  THEN  >in !
jwilke's avatar
jwilke committed
1291
    dup 0< s" prims" T $has? H 0= and
pazsan's avatar
pazsan committed
1292
    IF
jwilke's avatar
jwilke committed
1293
	.sourcepos ." needs prim: " >in @ bl word count type >in ! cr
pazsan's avatar
pazsan committed
1294 1295
    THEN
    (THeader over resolve T A, H 80 flag! ;
1296
: Alias:   ( cfa -- ) \ name
pazsan's avatar
pazsan committed
1297
    >in @ skip? IF  2drop  EXIT  THEN  >in !
jwilke's avatar
jwilke committed
1298
    dup 0< s" prims" T $has? H 0= and
pazsan's avatar
pazsan committed
1299
    IF
jwilke's avatar
jwilke committed
1300
	.sourcepos ." needs doer: " >in @ bl word count type >in ! cr
pazsan's avatar
pazsan committed
1301 1302
    THEN
    ghost tuck swap resolve <do:> swap >magic ! ;
1303 1304 1305 1306 1307

Variable prim#
: first-primitive ( n -- )  prim# ! ;
: Primitive  ( -- ) \ name
    prim# @ T Alias H  -1 prim# +! ;
anton's avatar
anton committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
>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
1335
  bl word gfind dup 0= ABORT" CROSS: Can't compile "
anton's avatar
anton committed
1336 1337 1338 1339 1340
  0> ( immediate? )
  IF    >exec @ compile,
  ELSE  postpone literal postpone gexecute  THEN ;
                                        immediate

jwilke's avatar
jwilke committed
1341 1342 1343 1344 1345 1346 1347 1348
: [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
1349
  dup undefined? ABORT" CROSS: forward " >link @ ;
jwilke's avatar
jwilke committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
               
>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
1368

jwilke's avatar
jwilke committed
1369
: fillcfa   ( usedcells -- )
1370
  T cells H xt>body swap - 0 ?DO 0 T c, tchar H +LOOP ;
jwilke's avatar
jwilke committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

: (>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,

jwilke's avatar
jwilke committed
1391 1392 1393
\ if we dont produce relocatable code alit, defaults to lit, jaw
has? relocate
[IF]
pazsan's avatar
pazsan committed
1394
: (alit,) ( n -- )  compile lit T  a, H ;	' (alit,) IS alit,
jwilke's avatar
jwilke committed
1395 1396 1397
[ELSE]
: (alit,) ( n -- )  lit, ;			' (alit,) IS alit,
[THEN]
jwilke's avatar
jwilke committed
1398 1399

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

pazsan's avatar
pazsan committed
1401 1402 1403 1404 1405
[IFUNDEF] (code) 
Defer (code)
Defer (end-code)
[THEN]

anton's avatar
anton committed
1406
>TARGET
pazsan's avatar
pazsan committed
1407
: Code
jwilke's avatar
jwilke committed
1408
  defempty?
anton's avatar
anton committed
1409
  (THeader there resolve
jwilke's avatar
jwilke committed
1410
  [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
jwilke's avatar
jwilke committed
1411
  doprim, 
anton's avatar
anton committed
1412 1413
  [THEN]
  depth (code) ;
pazsan's avatar
pazsan committed
1414 1415

: Code:
jwilke's avatar
jwilke committed
1416
  defempty?
anton's avatar
anton committed
1417
    ghost dup there ca>native resolve  <do:> swap >magic !
pazsan's avatar
pazsan committed
1418 1419 1420
    depth (code) ;

: end-code
jwilke's avatar
jwilke committed
1421
    (end-code)
pazsan's avatar
pazsan committed
1422 1423
    depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
    ELSE true ABORT" CROSS: Stack empty" THEN
jwilke's avatar
jwilke committed
1424
    ;
anton's avatar
anton committed
1425

1426
( Cond ) : chars tchar * ; ( Cond )
1427

anton's avatar
anton committed
1428 1429
>CROSS

jwilke's avatar
jwilke committed
1430
\ tLiteral                                             12dec92py
anton's avatar
anton committed
1431 1432

>TARGET
pazsan's avatar
pazsan committed
1433 1434
Cond: \G  T-\G ;Cond

anton's avatar
anton committed
1435 1436 1437 1438 1439 1440
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
1441 1442
\ some special literals					27jan97jaw

jwilke's avatar
jwilke committed
1443 1444 1445
\ !! 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
1446
Cond: MAXU
jwilke's avatar
jwilke committed
1447 1448 1449 1450 1451
  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
1452 1453

Cond: MINI
jwilke's avatar
jwilke committed
1454 1455 1456 1457 1458 1459 1460 1461
  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