cross.fs 67.9 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,2000 Free Software Foundation, Inc.
anton's avatar
anton committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

\ 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
20
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, 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

jwilke's avatar
jwilke committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
hex

\ debugging for compiling

\ print stack at each colon definition
\ : : save-input cr bl word count type restore-input throw .s : ;

\ print stack at each created word
\ : create save-input cr bl word count type restore-input throw .s create ;


\ \ -------------  Setup Vocabularies

\ Remark: Vocabulary is not ANS, but it should work...

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

jwilke's avatar
jwilke committed
56
: T  previous Ghosts also Target ; immediate
jwilke's avatar
jwilke committed
57 58 59 60 61
: G  Ghosts ; immediate
: H  previous Forth also Cross ; immediate

forth definitions

jwilke's avatar
jwilke committed
62
: T  previous Ghosts also Target ; immediate
jwilke's avatar
jwilke committed
63 64 65 66 67 68 69 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
: G  Ghosts ; immediate

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

H

>CROSS

\ find out whether we are compiling with gforth

: defined? bl word find nip ;
defined? emit-file defined? toupper and \ drop 0
[IF]
\ use this in a gforth system
: \GFORTH ; immediate
: \ANSI postpone \ ; immediate
[ELSE]
: \GFORTH postpone \ ; immediate
: \ANSI ; immediate
[THEN]

\ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
\ANSI : [IFDEF] defined? postpone [IF] ; immediate
0 \ANSI drop 1
[IF]
: \G postpone \ ; immediate
: rdrop postpone r> postpone drop ; immediate
: name bl word count ;
: bounds over + swap ;
: scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
: linked here over @ , swap ! ;
: alias create , DOES> @ EXECUTE ;
: defer ['] noop alias ;
: is state @ 
  IF ' >body postpone literal postpone ! 
  ELSE ' >body ! THEN ; immediate
: 0>= 0< 0= ;
: d<> rot <> -rot <> or ;
: toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
Variable ebuf
: emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
0a Constant #lf
0d Constant #cr

[IFUNDEF] Warnings Variable Warnings [THEN]

\ \ Number parsing					23feb93py

\ number? number                                       23feb93py

Variable dpl

hex
Create bases   10 ,   2 ,   A , 100 ,
\              16     2    10   character

\ !! protect BASE saving wrapper against exceptions
: getbase ( addr u -- addr' u' )
    over c@ [char] $ - dup 4 u<
    IF
	cells bases + @ base ! 1 /string
    ELSE
	drop
    THEN ;

: sign? ( addr u -- addr u flag )
    over c@ [char] - =  dup >r
    IF
	1 /string
    THEN
    r> ;

: s>unumber? ( addr u -- ud flag )
jwilke's avatar
jwilke committed
138 139 140 141
    over [char] ' =
    IF 	\ a ' alone is rather unusual :-)
	drop char+ c@ 0 true EXIT 
    THEN
jwilke's avatar
jwilke committed
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 172 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
    base @ >r  dpl on  getbase
    0. 2swap
    BEGIN ( d addr len )
	dup >r >number dup
    WHILE \ there are characters left
	dup r> -
    WHILE \ the last >number parsed something
	dup 1- dpl ! over c@ [char] . =
    WHILE \ the current char is '.'
	1 /string
    REPEAT  THEN \ there are unparseable characters left
	2drop false
    ELSE
	rdrop 2drop true
    THEN
    r> base ! ;

\ ouch, this is complicated; there must be a simpler way - anton
: s>number? ( addr len -- d f )
    \ converts string addr len into d, flag indicates success
    sign? >r
    s>unumber?
    0= IF
        rdrop false
    ELSE \ no characters left, all ok
	r>
	IF
	    dnegate
	THEN
	true
    THEN ;

: s>number ( addr len -- d )
    \ don't use this, there is no way to tell success
    s>number? drop ;

: snumber? ( c-addr u -- 0 / n -1 / d 0> )
    s>number? 0=
    IF
	2drop false  EXIT
    THEN
    dpl @ dup 0< IF
	nip
    ELSE
	1+
    THEN ;

: number? ( string -- string 0 / n -1 / d 0> )
    dup >r count snumber? dup if
	rdrop
    else
	r> swap
    then ;

: number ( string -- d )
    number? ?dup 0= abort" ?"  0<
    IF
	s>d
    THEN ;

[THEN]
anton's avatar
anton committed
203

jwilke's avatar
jwilke committed
204
hex     \ the defualt base for the cross-compiler is hex !!
jwilke's avatar
jwilke committed
205
\ Warnings off
jwilke's avatar
jwilke committed
206 207 208

\ words that are generaly useful

209
: KB  400 * ;
jwilke's avatar
jwilke committed
210 211 212 213 214
: >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
215

pazsan's avatar
pazsan committed
216 217 218
: string, ( c-addr u -- )
    \ puts down string as cstring
    dup c, here swap chars dup allot move ;
219

jwilke's avatar
jwilke committed
220 221
: ," [char] " parse string, ;

jwilke's avatar
jwilke committed
222
: SetValue ( n -- <name> )
pazsan's avatar
pazsan committed
223 224
\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
225
\G SetValue searches in the current vocabulary
jwilke's avatar
jwilke committed
226 227 228 229 230 231
  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
232
	set-order
jwilke's avatar
jwilke committed
233
  ELSE Value THEN ;
jwilke's avatar
jwilke committed
234 235

: DefaultValue ( n -- <name> )
pazsan's avatar
pazsan committed
236 237
\G Same behaviour as "Value" if the <name> is not defined
\G DefaultValue searches in the current vocabulary
jwilke's avatar
jwilke committed
238 239
 save-input bl word >r restore-input throw r> count
 get-current search-wordlist
240
 IF bl word drop 2drop ELSE Value THEN ;
anton's avatar
anton committed
241 242 243

hex

jwilke's avatar
jwilke committed
244 245 246
\ 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
247

jwilke's avatar
jwilke committed
248 249 250 251 252
: 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
253

jwilke's avatar
jwilke committed
254
\ Begin CROSS COMPILER:
anton's avatar
anton committed
255

jwilke's avatar
jwilke committed
256
\ debugging
jwilke's avatar
jwilke committed
257

jwilke's avatar
jwilke committed
258 259 260 261 262 263 264 265 266
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]

jwilke's avatar
jwilke committed
267 268
>CROSS

jwilke's avatar
jwilke committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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
298

jwilke's avatar
jwilke committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 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
\ \ --------------------	source file

decimal

Variable cross-file-list
0 cross-file-list !
Variable target-file-list
0 target-file-list !
Variable host-file-list
0 host-file-list !

cross-file-list Value file-list
0 Value source-desc

\ file loading

: >fl-id   1 cells + ;
: >fl-name 2 cells + ;

Variable filelist 0 filelist !
Create NoFile ," #load-file#"

: loadfile ( -- adr )
  source-desc ?dup IF >fl-name ELSE NoFile THEN ;

: sourcefilename ( -- adr len ) 
  loadfile count ;

\ANSI : sourceline# 0 ;

\ \ --------------------	path handling from kernel/paths.fs

\ paths.fs path file handling                                    03may97jaw

\ -Changing the search-path:
\ fpath+ <path> 	adds a directory to the searchpath
\ fpath= <path>|<path>	makes complete now searchpath
\ 			seperator is |
\ .fpath		displays the search path
\ remark I: 
\ a ./ in the beginning of filename is expanded to the directory the
\ current file comes from. ./ can also be included in the search-path!
\ ~+/ loads from the current working directory

\ remark II:
\ if there is no sufficient space for the search path increase it!


\ -Creating custom paths:

\ It is possible to use the search mechanism on yourself.

\ Make a buffer for the path:
\ create mypath	100 chars , 	\ maximum length (is checked)
\ 		0 ,		\ real len
\ 		100 chars allot \ space for path
\ use the same functions as above with:
\ mypath path+ 
\ mypath path=
\ mypath .path

\ do a open with the search path:
\ open-path-file ( adr len path -- fd adr len ior )
\ the file is opened read-only; if the file is not found an error is generated

\ questions to: wilke@jwdt.com

[IFUNDEF] +place
: +place ( adr len adr )
        2dup >r >r
        dup c@ char+ + swap move
        r> r> dup c@ rot + swap c! ;
[THEN]

[IFUNDEF] place
: place ( c-addr1 u c-addr2 )
        2dup c! char+ swap move ;
[THEN]

\ if we have path handling, use this and the setup of it
[IFUNDEF] open-fpath-file

create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
sourcepath value fpath

: also-path ( adr len path^ -- )
  >r
  \ len check
  r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
  \ copy into
  tuck r@ cell+ dup @ cell+ + swap cmove
  \ make delimiter
  0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
  ;

: only-path ( adr len path^ -- )
  dup 0 swap cell+ ! also-path ;

: path+ ( path-addr  "dir" -- ) \ gforth
    \G Add the directory @var{dir} to the search path @var{path-addr}.
    name rot also-path ;

: fpath+ ( "dir" ) \ gforth
    \G Add directory @var{dir} to the Forth search path.
    fpath path+ ;

: path= ( path-addr "dir1|dir2|dir3" ) \ gforth
    \G Make a complete new search path; the path separator is |.
    name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
    rot only-path ;

: fpath= ( "dir1|dir2|dir3" ) \ gforth
    \G Make a complete new Forth search path; the path separator is |.
    fpath path= ;

: path>counted  cell+ dup cell+ swap @ ;

: next-path ( adr len -- adr2 len2 )
  2dup 0 scan
  dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
  >r 1+ -rot r@ 1- -rot
  r> - ;

: previous-path ( path^ -- )
  dup path>counted
  BEGIN tuck dup WHILE repeat ;

: .path ( path-addr -- ) \ gforth
    \G Display the contents of the search path @var{path-addr}.
    path>counted
    BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;

: .fpath ( -- ) \ gforth
    \G Display the contents of the Forth search path.
    fpath .path ;

: absolut-path? ( addr u -- flag ) \ gforth
    \G A path is absolute if it starts with a / or a ~ (~ expansion),
    \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
    \G it has a colon as second character ("C:...").  Paths simply
    \G containing a / are not absolute!
    2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
    over c@ [char] / = >r
    over c@ [char] ~ = >r
    \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
    2 min S" ./" compare 0=
    r> r> r> or or or ;

Create ofile 0 c, 255 chars allot
Create tfile 0 c, 255 chars allot

: pathsep? dup [char] / = swap [char] \ = or ;

: need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;

: extractpath ( adr len -- adr len2 )
  BEGIN dup WHILE 1-
        2dup + c@ pathsep? IF EXIT THEN
  REPEAT ;

: remove~+ ( -- )
    ofile count 3 min s" ~+/" compare 0=
    IF
	ofile count 3 /string ofile place
    THEN ;

: expandtopic ( -- ) \ stack effect correct? - anton
    \ expands "./" into an absolute name
    ofile count 2 min s" ./" compare 0=
    IF
	ofile count 1 /string tfile place
	0 ofile c! sourcefilename extractpath ofile place
	ofile c@ IF need/ THEN
	tfile count over c@ pathsep? IF 1 /string THEN
	ofile +place
    THEN ;

: compact.. ( adr len -- adr2 len2 )
pazsan's avatar
pazsan committed
477 478 479 480 481 482 483 484 485 486 487
    \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
    over swap
    BEGIN  dup  WHILE
        dup >r '/ scan 2dup 4 min s" /../" compare 0=
        IF
            dup r> - >r 4 /string over r> + 4 -
            swap 2dup + >r move dup r> over -
        ELSE
            rdrop dup 1 min /string
        THEN
    REPEAT  drop over - ;
jwilke's avatar
jwilke committed
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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

: reworkdir ( -- )
  remove~+
  ofile count compact..
  nip ofile c! ;

: open-ofile ( -- fid ior )
    \G opens the file whose name is in ofile
    expandtopic reworkdir
    ofile count r/o open-file ;

: check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
  0 ofile ! >r >r ofile place need/
  r> r> ofile +place
  open-ofile ;

: open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
    \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
    \G If found, the resulting path and an open file descriptor
    \G are returned. If the file is not found, @var{ior} is non-zero.
  >r
  2dup absolut-path?
  IF    rdrop
        ofile place open-ofile
	dup 0= IF >r ofile count r> THEN EXIT
  ELSE  r> path>counted
        BEGIN  next-path dup
        WHILE  5 pick 5 pick check-path
        0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
  REPEAT
        2drop 2drop 2drop -38
  THEN ;

: open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
    \G Look in the Forth search path for the file specified by @var{addr1 u1}.
    \G If found, the resulting path and an open file descriptor
    \G are returned. If the file is not found, @var{ior} is non-zero.
    fpath open-path-file ;

fpath= ~+

[THEN]

\ \ --------------------	include require			13may99jaw

>CROSS

: add-included-file ( adr len -- adr )
  dup >fl-name char+ allocate throw >r
  file-list @ r@ ! r@ file-list !
  r@ >fl-name place r> ;

: included? ( c-addr u -- f )
  file-list
  BEGIN	@ dup
  WHILE	>r 2dup r@ >fl-name count compare 0=
	IF rdrop 2drop true EXIT THEN
	r>
  REPEAT
  2drop drop false ;	

false DebugFlag showincludedfiles

: included1 ( fd adr u -- )
\ include file adr u / fd
\ we don't use fd with include-file, because the forth system
\ doesn't know the name of the file to get a nice error report
  [d?] showincludedfiles
  IF	cr ." Including: " 2dup type ." ..." THEN
  rot close-file throw
  source-desc >r
  add-included-file to source-desc 
  sourcefilename
  ['] included catch
  r> to source-desc 
  throw ;

: included ( adr len -- )
	cross-file-list to file-list
	open-fpath-file throw 
        included1 ;

: required ( adr len -- )
	cross-file-list to file-list
	open-fpath-file throw \ 2dup cr ." R:" type
	2dup included?
	IF 	2drop close-file throw
	ELSE	included1
	THEN ;

: include bl word count included ;

: require bl word count required ;

jwilke's avatar
jwilke committed
582 583
0 [IF]

jwilke's avatar
jwilke committed
584 585 586 587 588 589 590 591 592 593
also forth definitions previous

: included ( adr len -- ) included ;

: required ( adr len -- ) required ;

: include include ;

: require require ;

jwilke's avatar
jwilke committed
594 595
[THEN]

jwilke's avatar
jwilke committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
>CROSS
hex

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

previous >CROSS

: .dec
  base @ decimal swap . base ! ;

: .sourcepos
  cr sourcefilename type ." :"
  sourceline# .dec ;

: 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
631 632 633 634 635 636 637 638
\ \ GhostNames Ghosts                                  9may93jaw

\ second name source to search trough list

VARIABLE GhostNames
0 GhostNames !

: GhostName ( -- addr )
639
    align here GhostNames @ , GhostNames ! here 0 ,
jwilke's avatar
jwilke committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653
    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
654
hex
jwilke's avatar
jwilke committed
655 656
4711 Constant <fwd>             4712 Constant <res>
4713 Constant <imm>             4714 Constant <do:>
657
4715 Constant <skip>
jwilke's avatar
jwilke committed
658

659
\  Compiler States
jwilke's avatar
jwilke committed
660

661 662 663 664 665
Variable comp-state
0 Constant interpreting
1 Constant compiling
2 Constant resolving
3 Constant assembling
jwilke's avatar
jwilke committed
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680
Defer lit, ( n -- )
Defer alit, ( n -- )

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

Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
681 682
Defer prim, ( tcfa -- )                 \ compiles a primitive invocation
					\ at current position
683 684 685 686 687
Defer colonmark, ( -- addr )		\ marks a colon call
Defer colon-resolve ( tcfa addr -- )

Defer addr-resolve ( target-addr addr -- )
Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
jwilke's avatar
jwilke committed
688

689 690 691 692 693 694 695 696 697 698 699 700
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 )

[IFUNDEF] ca>native
defer ca>native	
[THEN]

\ ghost structure
jwilke's avatar
jwilke committed
701 702 703 704 705

: >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
706 707
: >comp 3 cells + ;     \ compilation semantics
: >end 4 cells + ;	\ room for additional tags
jwilke's avatar
jwilke committed
708 709 710
			\ for builder (create, variable...) words the
			\ execution symantics of words built are placed here

711 712 713 714 715 716 717 718 719 720 721
\ resolve structure

: >next ;		\ link to next field
: >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
: >taddr cell+ cell+ ;	
: >ghost 3 cells + ;
: >file 4 cells + ;
: >line 5 cells + ;

\ refer variables

jwilke's avatar
jwilke committed
722 723 724 725
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

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
: (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# , 
  ;

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

Variable atonce atonce off

: NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;

: is-forward   ( ghost -- )
  colonmark, 0 (refered) ; \ compile space for call

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

jwilke's avatar
jwilke committed
748 749 750 751 752 753 754 755 756 757 758 759
: 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
jwilke's avatar
jwilke committed
760
  dup count [ ' ghosts >wordlist ] Literal search-wordlist
jwilke's avatar
jwilke committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
  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
jwilke's avatar
jwilke committed
777
  >in @  bl word gfind   IF  atonce off Already on nip EXIT  THEN
jwilke's avatar
jwilke committed
778 779 780 781 782 783 784
  drop  >in !  Make-Ghost ;

: >ghostname ( ghost -- adr len )
  GhostNames
  BEGIN @ dup
  WHILE 2dup cell+ @ =
  UNTIL nip 2 cells + count
pazsan's avatar
pazsan committed
785 786 787
  ELSE  2drop 
	\ true abort" CROSS: Ghostnames inconsistent"
	s" ?!?!?!"
jwilke's avatar
jwilke committed
788 789
  THEN ;

jwilke's avatar
jwilke committed
790 791
: .ghost ( ghost -- ) >ghostname type ;

jwilke's avatar
jwilke committed
792
\ ' >ghostname ALIAS @name
jwilke's avatar
jwilke committed
793 794 795 796

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

797 798 799
: undefined? ( ghost -- flag )
  >magic @ dup <fwd> = swap <skip> = or ;

jwilke's avatar
jwilke committed
800 801 802 803 804 805 806 807 808 809 810
\ 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
pazsan's avatar
pazsan committed
811
ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
jwilke's avatar
jwilke committed
812 813
ghost '                                         drop
ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
814
ghost :dovar	ghost :dodefer  ghost :dofield  2drop drop
jwilke's avatar
jwilke committed
815
ghost over      ghost =         ghost drop      2drop drop
816 817
ghost call      ghost useraddr  ghost execute   2drop drop
ghost +         ghost -         ghost @         2drop drop
pazsan's avatar
pazsan committed
818 819
ghost 2drop drop
ghost 2dup drop
jwilke's avatar
jwilke committed
820 821 822 823 824 825 826 827 828 829 830 831 832

\ \ 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
833
: environment? ( adr len -- [ x ] true | false )
jwilke's avatar
jwilke committed
834 835 836
  target-environment search-wordlist 
  IF execute true ELSE false THEN ;

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

jwilke's avatar
jwilke committed
839
: has? 	bl word count T environment? H 
jwilke's avatar
jwilke committed
840 841
	IF 	\ environment variable is present, return its value
	ELSE	\ environment variable is not present, return false
842
		false \ debug true ABORT" arg" 
jwilke's avatar
jwilke committed
843
	THEN ;
jwilke's avatar
jwilke committed
844 845 846

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

pazsan's avatar
pazsan committed
847 848
>ENVIRON get-order get-current swap 1+ set-order
true SetValue compiler
849
true SetValue cross
pazsan's avatar
pazsan committed
850 851
true SetValue standard-threading
>TARGET previous
jwilke's avatar
jwilke committed
852

jwilke's avatar
jwilke committed
853 854 855 856 857 858
0
[IFDEF] mach-file mach-file count 1 [THEN]
[IFDEF] machine-file machine-file 1 [THEN]
[IF] 	included hex drop
[ELSE]  cr ." No machine description!" ABORT 
[THEN]
jwilke's avatar
jwilke committed
859

jwilke's avatar
jwilke committed
860 861
>ENVIRON

pazsan's avatar
pazsan committed
862 863 864 865 866 867 868 869 870 871 872 873
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
874
false DefaultValue backtrace
875
false DefaultValue new-input
pazsan's avatar
pazsan committed
876 877 878 879 880
[THEN]

true DefaultValue interpreter
true DefaultValue ITC
false DefaultValue rom
jwilke's avatar
jwilke committed
881
true DefaultValue standardthreading
jwilke's avatar
jwilke committed
882

jwilke's avatar
jwilke committed
883 884 885 886 887
>TARGET
s" relocate" T environment? H 
[IF]	SetValue NIL
[ELSE]	>ENVIRON T NIL H SetValue relocate
[THEN]
pazsan's avatar
pazsan committed
888 889 890

>CROSS

jwilke's avatar
jwilke committed
891
\ \ Create additional parameters                         19jan95py
pazsan's avatar
pazsan committed
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
\ 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
910
\ this sets byte size for the target machine, (probably right guess) jaw
jwilke's avatar
jwilke committed
911

pazsan's avatar
pazsan committed
912
T
913 914 915 916 917 918 919 920 921 922 923 924 925 926
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
927
H
928
tbits/char bits/byte /	Constant tbyte
929

pazsan's avatar
pazsan committed
930

anton's avatar
anton committed
931 932 933 934 935 936 937 938
\ 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
939 940 941 942 943
\ statistics						10jun97jaw

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

jwilke's avatar
jwilke committed
944 945 946
: target>bitmask-size ( u1 -- u2 )
  1- tcell>bit rshift 1+ ;

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

pazsan's avatar
pazsan committed
951
\ \ memregion.fs
jwilke's avatar
jwilke committed
952 953 954 955 956 957 958 959


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
960 961 962 963
: >rname 6 cells + ;
: >rbm   5 cells + ;
: >rmem  4 cells + ;
: >rlink 3 cells + ;
jwilke's avatar
jwilke committed
964 965 966 967 968 969 970 971 972 973 974 975 976
: >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
977
	region-link linked 0 , 0 , bl word count string,
jwilke's avatar
jwilke committed
978 979 980
  ELSE	\ store new parameters in region
        bl word drop
	>body >r r@ last-defined-region !
jwilke's avatar
jwilke committed
981
	r@ >rlen ! dup r@ >rstart ! r> >rdp !
jwilke's avatar
jwilke committed
982 983 984
  THEN ;

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

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

: area ( region -- startaddr totallen ) \G returns the total area
991
  dup >rstart @ swap >rlen @ ;
jwilke's avatar
jwilke committed
992 993 994

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

jwilke's avatar
jwilke committed
997 998
: .addr ( u -- )
\G prints a 16 or 32 Bit nice hex value
jwilke's avatar
jwilke committed
999 1000
  base @ >r hex
  tcell 2 u>
jwilke's avatar
jwilke committed
1001
  IF s>d <# # # # # [char] . hold # # # # #> type
jwilke's avatar
jwilke committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  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
1012 1013 1014
  WHILE cr
        0 >rlink - >r
        r@ >rname count tuck type
jwilke's avatar
jwilke committed
1015
        12 swap - 0 max spaces space
jwilke's avatar
jwilke committed
1016 1017 1018
        ." Start: " r@ >rstart @ dup .addr space
        ." End: " r@ >rlen @ + .addr space
        ." DP: " r> >rdp @ .addr
jwilke's avatar
jwilke committed
1019
  REPEAT drop
jwilke's avatar
jwilke committed
1020
  s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
1021 1022 1023
  cr ." Mirrored:"
  mirrored-link @
  BEGIN dup
jwilke's avatar
jwilke committed
1024
  WHILE space dup cell+ @ >rname count type @
jwilke's avatar
jwilke committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  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
1036
T has? rom H
jwilke's avatar
jwilke committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
[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


1053
: setup-target ( -- )   \G initialize target's memory space
jwilke's avatar
jwilke committed
1054
  s" rom" T $has? H
jwilke's avatar
jwilke committed
1055
  IF  \ check for ram and rom...
1056
      \ address-space area nip 0<>
jwilke's avatar
jwilke committed
1057 1058
      ram-dictionary area nip 0<>
      rom-dictionary area nip 0<>
1059
      and 0=
jwilke's avatar
jwilke committed
1060 1061 1062 1063 1064 1065 1066 1067
      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
1068
  nip 0=
jwilke's avatar
jwilke committed
1069
  ABORT" CROSS: define at least address-space or dictionary!!"
jwilke's avatar
jwilke committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

  \ 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
1084
                dup bit$ !
jwilke's avatar
jwilke committed
1085 1086 1087
                r> >rbm !

        ELSE    r> drop THEN
1088 1089
   REPEAT drop ;

1090
\ MakeKernel                                           		22feb99jaw
1091 1092 1093 1094 1095 1096 1097

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

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

pazsan's avatar
pazsan committed
1099
\ \ switched tdp for rom support				03jun97jaw
jwilke's avatar
jwilke committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

\ 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
1133 1134 1135 1136
: activate ( region -- )
\G next code goes to this region
  >rdp to tdp ;

jwilke's avatar
jwilke committed
1137
: (switchram)
jwilke's avatar
jwilke committed
1138
  fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
1139
  ram-dictionary activate ;
jwilke's avatar
jwilke committed
1140 1141 1142 1143 1144 1145

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

: switchrom
jwilke's avatar
jwilke committed
1146
  fixed @ ?EXIT rom-dictionary activate ;
jwilke's avatar
jwilke committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

: >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
1162
\ default compilation goes to rom
jwilke's avatar
jwilke committed
1163 1164 1165 1166
\ when romable support is off, only the rom switch is used (!!)
>auto

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

anton's avatar
anton committed
1168 1169
>TARGET

jwilke's avatar
jwilke committed
1170 1171
\ \ Target Memory Handling

anton's avatar
anton committed
1172 1173
\ Byte ordering and cell size                          06oct92py

pazsan's avatar
pazsan committed
1174 1175
: cell+         tcell + ;
: cells         tcell<< lshift ;
1176 1177
: chars         tchar * ;
: char+		tchar + ;
pazsan's avatar
pazsan committed
1178
: floats	tfloat * ;
1179
    
anton's avatar
anton committed
1180
>CROSS
pazsan's avatar
pazsan committed
1181
: cell/         tcell<< rshift ;
anton's avatar
anton committed
1182 1183
>TARGET
20 CONSTANT bl
jwilke's avatar
jwilke committed
1184
\ TNIL Constant NIL
anton's avatar
anton committed
1185 1186 1187

>CROSS

1188 1189
bigendian
[IF]
jwilke's avatar
jwilke committed
1190
   : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
1191
     DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
jwilke's avatar
jwilke committed
1192
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds
1193
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
1194 1195 1196 1197
   : 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
1198
[ELSE]
jwilke's avatar
jwilke committed
1199
   : S!  ( n addr -- )  >r s>d r> tcell bounds
1200
     DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
jwilke's avatar
jwilke committed
1201
   : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
1202
     DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
1203 1204 1205 1206
   : 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
1207 1208
[THEN]

jwilke's avatar
jwilke committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
: 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
1235 1236 1237 1238 1239 1240 1241
\ 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
1242
: -bit ( addr n -- )  >bit invert over c@ and swap c! ;
jwilke's avatar
jwilke committed
1243

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
: (relon) ( taddr -- )  
  [ [IFDEF] fd-relocation-table ]
  s" +" fd-relocation-table write-file throw
  dup s>d <# #s #> fd-relocation-table write-line throw
  [ [THEN] ]
  bit$ @ swap cell/ +bit ;

: (reloff) ( taddr -- ) 
  [ [IFDEF] fd-relocation-table ]
  s" -" fd-relocation-table write-file throw
  dup s>d <# #s #> fd-relocation-table write-line throw
  [ [THEN] ]
  bit$ @ swap cell/ -bit ;
jwilke's avatar
jwilke committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

: (>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
1273
' (>regionimage) IS >image
jwilke's avatar
jwilke committed
1274
[THEN]
anton's avatar
anton committed
1275 1276 1277 1278

\ Target memory access                                 06oct92py

: align+  ( taddr -- rest )
anton's avatar
anton committed
1279
    tcell tuck 1- and - [ tcell 1- ] Literal and ;
1280
: cfalign+  ( taddr -- rest )
pazsan's avatar
pazsan committed
1281
    \ see kernel.fs:cfaligned
pazsan's avatar
pazsan committed
1282
    /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
anton's avatar
anton committed
1283 1284 1285 1286 1287

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

1288
: cfaligned ( taddr1 -- taddr2 )
pazsan's avatar
pazsan committed
1289
    \ see kernel.fs
1290 1291
    dup cfalign+ + ;

jwilke's avatar
jwilke committed
1292 1293
: @  ( taddr -- w )     >image S@ ;
: !  ( w taddr -- )     >image S! ;
1294 1295
: c@ ( taddr -- char )  >image Sc@ ;
: c! ( char taddr -- )  >image Sc! ;
1296
: 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
1297
: 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
anton's avatar
anton committed
1298 1299 1300 1301 1302 1303

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

: here  ( -- there )    there ;
: allot ( n -- )        tdp +! ;
jwilke's avatar
jwilke committed
1304 1305 1306
: ,     ( w -- )        T here H tcell T allot  ! H ;
: c,    ( char -- )     T here H tchar T allot c! H ;
: align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
1307
: cfalign ( -- )
jwilke's avatar
jwilke committed
1308
    T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
anton's avatar
anton committed
1309

1310
: >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
1311 1312
: A!                    swap >address swap dup relon T ! H ;
: A,    ( w -- )        >address T here H relon T , H ;
anton's avatar
anton committed
1313 1314 1315

>CROSS

jwilke's avatar
jwilke committed
1316 1317
: tcmove ( source dest len -- )
\G cmove in target memory
1318
  tchar * bounds
jwilke's avatar
jwilke committed
1319
  ?DO  dup T c@ H I T c! H 1+
1320
  tchar +LOOP  drop ;
anton's avatar
anton committed
1321

jwilke's avatar
jwilke committed
1322 1323
\ \ Load Assembler

anton's avatar
anton committed
1324
>TARGET
jwilke's avatar
jwilke committed
1325
H also Forth definitions
anton's avatar
anton committed
1326

jwilke's avatar
jwilke committed
1327
: X 	bl word count [ ' target >wordlist ] Literal search-wordlist
jwilke's avatar
jwilke committed
1328 1329
	IF	state @ IF compile,
		ELSE execute THEN
jwilke's avatar
jwilke committed
1330 1331
	ELSE	-1 ABORT" Cross: access method not supported!"
	THEN ; immediate
anton's avatar
anton committed
1332

jwilke's avatar
jwilke committed
1333
[IFDEF] asm-include asm-include [THEN] hex
anton's avatar
anton committed
1334

jwilke's avatar
jwilke committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
previous
>CROSS H

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

>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
1345 1346
DEFER doprim,	\ compiles start of a primitive
DEFER docol,   	\ compiles start of a colon definition
jwilke's avatar
jwilke committed
1347 1348 1349 1350
DEFER doer,		
DEFER fini,      \ compiles end of definition ;s
DEFER doeshandler,
DEFER dodoes,
anton's avatar
anton committed
1351

jwilke's avatar
jwilke committed
1352 1353
DEFER ]comp     \ starts compilation
DEFER comp[     \ ends compilation
anton's avatar
anton committed
1354

1355
: (prim) T a, H ;				' (prim) IS prim,
pazsan's avatar
pazsan committed
1356

1357
: (cr) >tempdp ]comp prim, comp[ tempdp> ; 	' (cr) IS colon-resolve
pazsan's avatar
pazsan committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
: (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
1369
    -1 prim, ;					' (cm) IS colonmark,
anton's avatar
anton committed
1370

jwilke's avatar
jwilke committed
1371
>TARGET
1372
: compile, prim, ;
jwilke's avatar
jwilke committed
1373
>CROSS
anton's avatar
anton committed
1374

jwilke's avatar
jwilke committed
1375
: refered ( ghost tag -- )
jwilke's avatar
jwilke committed
1376
\G creates a resolve structure
pazsan's avatar
pazsan committed
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    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
1390
  ;
anton's avatar
anton committed
1391

jwilke's avatar
jwilke committed
1392
Defer resolve-warning
anton's avatar
anton committed
1393

jwilke's avatar
jwilke committed
1394
: reswarn-test ( ghost res-struct -- ghost res-struct )
jwilke's avatar
jwilke committed
1395
  over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
anton's avatar
anton committed
1396

jwilke's avatar
jwilke committed
1397
: reswarn-forward ( ghost res-struct -- ghost res-struct )
jwilke's avatar
jwilke committed
1398 1399
  over warnhead .ghost dup ."  is referenced in " 
  >ghost @ .ghost ;
anton's avatar
anton committed
1400

jwilke's avatar
jwilke committed
1401 1402
\ ' reswarn-test IS resolve-warning
 
anton's avatar
anton committed
1403 1404
\ resolve                                              14oct92py

pazsan's avatar
pazsan committed
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
 : 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
1419 1420 1421 1422

\ : 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
1423 1424 1425

\ exists                                                9may93jaw

jwilke's avatar
jwilke committed
1426 1427 1428 1429 1430
Variable TWarnings
TWarnings on
Variable Exists-Warnings
Exists-Warnings on

anton's avatar
anton committed
1431 1432 1433 1434 1435
: exists ( ghost tcfa -- )
  over GhostNames
  BEGIN @ dup
  WHILE 2dup cell+ @ =
  UNTIL
jwilke's avatar
jwilke committed
1436 1437 1438 1439 1440
        2 cells + count
        TWarnings @ Exists-Warnings @ and
        IF warnhead type ."  exists"
        ELSE 2drop THEN
        drop swap >link !
pazsan's avatar
pazsan committed
1441
  ELSE  true abort" CROSS: Ghostnames inconsistent "
anton's avatar
anton committed
1442 1443
  THEN ;

1444 1445 1446 1447
: colon-resolved   ( ghost -- )
    >link @ colon, ; \ compile-call
: prim-resolved  ( ghost -- )
    >link @ prim, ;
1448

anton's avatar
anton committed
1449
: resolve  ( ghost tcfa -- )
pazsan's avatar
pazsan committed
1450 1451 1452
\G resolve referencies to ghost with tcfa
    \ is ghost resolved?, second resolve means another definition with the
    \ same name
1453
    over undefined? 0= IF  exists EXIT THEN
pazsan's avatar
pazsan committed
1454 1455 1456 1457
    \ get linked-list
    swap >r r@ >link @ swap \ ( list tcfa R: ghost )
    \ mark ghost as resolved
    dup r@ >link ! <res> r@ >magic !
1458 1459
    r@ >comp @ ['] is-forward = IF
	['] prim-resolved  r@ >comp !  THEN
pazsan's avatar
pazsan committed
1460 1461 1462 1463 1464 1465 1466
    \ 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
1467
  ;
anton's avatar
anton committed
1468 1469 1470

\ gexecute ghost,                                      01nov92py

jwilke's avatar
jwilke committed
1471
: gexecute   ( ghost -- )
1472
    dup >comp @ execute ;
jwilke's avatar
jwilke committed
1473 1474

: addr,  ( ghost -- )
1475
  dup forward? IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
anton's avatar
anton committed
1476

jwilke's avatar
jwilke committed
1477
\ !! : ghost,     ghost  gexecute ;
anton's avatar
anton committed
1478 1479 1480 1481 1482 1483 1484

\ .unresolved                                          11may93jaw

variable ResolveFlag

\ ?touched                                             11may93jaw

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

jwilke's avatar
jwilke committed
1488 1489 1490 1491 1492
: .forwarddefs ( ghost -- )
	."  appeared in:"
	>link
	BEGIN	@ dup
	WHILE	cr 5 spaces
jwilke's avatar
jwilke committed
1493
		dup >ghost @ .ghost
jwilke's avatar
jwilke committed
1494 1495 1496 1497 1498
		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
		."  line " dup >line @ .dec
	REPEAT 
	drop ;

anton's avatar
anton committed
1499 1500
: ?resolved  ( ghostname -- )
  dup cell+ @ ?touched
jwilke's avatar
jwilke committed
1501 1502 1503 1504 1505
  IF  	dup 
	cell+ cell+ count cr type ResolveFlag on 
	cell+ @ .forwarddefs
  ELSE 	drop 
  THEN ;
anton's avatar
anton committed
1506 1507 1508 1509 1510 1511

: .unresolved  ( -- )
  ResolveFlag off cr ." Unresolved: "
  Ghostnames
  BEGIN @ dup
  WHILE dup ?resolved
anton's avatar
anton committed
1512 1513
  REPEAT drop ResolveFlag @
  IF
anton's avatar
anton committed
1514
      -1 abort" Unresolved words!"
anton's avatar
anton committed
1515 1516 1517 1518
  ELSE
      ." Nothing!"
  THEN
  cr ;
anton's avatar
anton committed
1519

jwilke's avatar
jwilke committed
1520 1521 1522 1523 1524
: .stats
  base @ >r decimal
  cr ." named Headers: " headers-named @ . 
  r> base ! ;

1525 1526 1527 1528
>MINIMAL

: .unresolved .unresolved ;

anton's avatar
anton committed
1529 1530 1531
>CROSS
\ Header states                                        12dec92py

pazsan's avatar
pazsan committed
1532 1533
bigendian [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
: flag! ( w -- )   tlast @ flag+ + dup >r T c@ xor r> c! H ;
anton's avatar
anton committed
1534 1535 1536

VARIABLE ^imm

anton's avatar
anton committed
1537
\ !! should be target wordsize specific
pazsan's avatar
pazsan committed
1538 1539 1540
$80 constant alias-mask
$40 constant immediate-mask
$20 constant restrict-mask
anton's avatar
anton committed
1541

anton's avatar
anton committed
1542
>TARGET
anton's avatar
anton committed
1543
: immediate     immediate-mask flag!
1544
                ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
anton's avatar
anton committed
1545 1546
                <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                <imm> ^imm @ ! ;
anton's avatar
anton committed
1547
: restrict      restrict-mask flag! ;
jwilke's avatar
jwilke committed
1548

pazsan's avatar
pazsan committed
1549 1550 1551 1552
: 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
1553 1554 1555 1556
>CROSS

\ Target Header Creation                               01nov92py

jwilke's avatar
jwilke committed
1557
>TARGET
anton's avatar
anton committed
1558
: string,  ( addr count -- )
anton's avatar
anton committed
1559 1560 1561 1562
    dup T c, H bounds  ?DO  I c@ T c, H  LOOP ;
: lstring, ( addr count -- )
    dup T , H bounds  ?DO  I c@ T c, H  LOOP ;
: name,  ( "name" -- )  bl word count T lstring, cfalign H ;
anton's avatar
anton committed
1563
: view,   ( -- ) ( dummy ) ;
jwilke's avatar
jwilke committed
1564
>CROSS
anton's avatar
anton committed
1565

1566 1567
\ Target Document Creation (goes to crossdoc.fd)       05jul95py

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

pazsan's avatar
pazsan committed
1571
: T-\G ( -- )
1572
    source >in @ /string doc-file-id write-line throw
pazsan's avatar
pazsan committed
1573
    postpone \ ;
1574

pazsan's avatar
pazsan committed
1575
Variable to-doc  to-doc on
1576 1577 1578 1579 1580 1581

: 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
1582
        Last-Header-Ghost @ >ghostname doc-file-id write-file throw
1583 1584 1585 1586 1587
	>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
1588
	T-\G
1589
	>in !
pazsan's avatar
pazsan committed
1590
    THEN ;
1591

pazsan's avatar
pazsan committed
1592 1593
\ Target TAGS creation

pazsan's avatar
pazsan committed
1594
s" kernel.TAGS" r/w create-file throw value tag-file-id
pazsan's avatar
pazsan committed
1595 1596 1597 1598 1599 1600 1601 1602 1603
\ 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 ( -- )
jwilke's avatar
jwilke committed
1604
    sourcefilename last-loadfilename 2@ d<>
pazsan's avatar
pazsan committed
1605 1606
    IF
	tag-bof count tag-file-id write-line throw
1607
	sourcefilename 2dup
pazsan's avatar
pazsan committed
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	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
jwilke's avatar
jwilke committed
1619
	tlast @ >image count 1F and tag-file-id write-file throw
pazsan's avatar
pazsan committed
1620
	tag-end count tag-file-id write-file throw
1621
	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
pazsan's avatar
pazsan committed
1622 1623 1624 1625 1626
\	>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
1627 1628 1629 1630
\ Check for words

Defer skip? ' false IS skip?

1631
: skipdef ( <name> -- )
<