cross.fs 112 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

Anton Ertl's avatar
Anton Ertl committed
4
\ Copyright (C) 1995,1996,1997,1998,1999,2000,2003,2004,2005,2006,2007,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 Free Software Foundation, Inc.
anton's avatar
anton committed
5 6 7 8 9

\ 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
10
\ as published by the Free Software Foundation, either version 3
anton's avatar
anton committed
11 12 13 14 15 16 17 18
\ 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
19
\ along with this program. If not, see http://www.gnu.org/licenses/.
anton's avatar
anton committed
20

jwilke's avatar
jwilke committed
21 22 23 24
0 
[IF]

ToDo:
jwilke's avatar
jwilke committed
25 26 27 28
- Crossdoc destination ./doc/crossdoc.fd makes no sense when
  cross.fs is used seperately. jaw
- Do we need this char translation with >address and in branchoffset? 
  (>body also affected) jaw
jwilke's avatar
jwilke committed
29 30

[THEN]
anton's avatar
anton committed
31

32 33
s" compat/strcomp.fs" included

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
: G  Ghosts ; immediate

65

jwilke's avatar
jwilke committed
66 67 68 69 70 71 72 73
: >cross  also Cross definitions previous ;
: >target also Target definitions previous ;
: >minimal also Minimal definitions previous ;

H

>CROSS

jwilke's avatar
jwilke committed
74
\ Test against this definitions to find out whether we are cross-compiling
75
\ may be useful for assemblers
jwilke's avatar
jwilke committed
76 77
0 Constant gforth-cross-indicator

jwilke's avatar
jwilke committed
78 79
\ find out whether we are compiling with gforth

80 81 82
: bl-word ( -- addr )
    parse-name here place here ;
: defined? bl-word find nip ;
jwilke's avatar
jwilke committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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
99
: name bl-word count ;
jwilke's avatar
jwilke committed
100 101 102 103 104 105 106 107 108 109
: 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 ;
110
: toupper dup 'a' 'z' 1+ within IF 'A' 'a' - + THEN ;
jwilke's avatar
jwilke committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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' )
130
    over c@ '$' - dup 4 u<
jwilke's avatar
jwilke committed
131 132 133 134 135 136 137
    IF
	cells bases + @ base ! 1 /string
    ELSE
	drop
    THEN ;

: sign? ( addr u -- addr u flag )
138
    over c@ '-' =  dup >r
jwilke's avatar
jwilke committed
139 140 141 142 143 144
    IF
	1 /string
    THEN
    r> ;

: s>unumber? ( addr u -- ud flag )
145
    over ''' =
jwilke's avatar
jwilke committed
146 147 148
    IF 	\ a ' alone is rather unusual :-)
	drop char+ c@ 0 true EXIT 
    THEN
jwilke's avatar
jwilke committed
149 150 151 152 153 154 155
    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
156
	dup 1- dpl ! over c@ '.' =
jwilke's avatar
jwilke committed
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
    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 ;

196
: (number?) ( string -- string 0 / n -1 / d 0> )
jwilke's avatar
jwilke committed
197 198 199 200 201 202 203
    dup >r count snumber? dup if
	rdrop
    else
	r> swap
    then ;

: number ( string -- d )
204
    (number?) ?dup 0= abort" ?"  0<
jwilke's avatar
jwilke committed
205 206 207 208 209
    IF
	s>d
    THEN ;

[THEN]
anton's avatar
anton committed
210

211 212
[IFUNDEF] (number?) : (number?) number? ; [THEN]

213 214 215 216
\ this provides assert( and struct stuff
\GFORTH [IFUNDEF] assert1(
\GFORTH also forth definitions require assert.fs previous
\GFORTH [THEN]
Bernd Paysan's avatar
Bernd Paysan committed
217 218 219
[IFUNDEF] $@
    : $@ @ dup IF  dup cell+ swap @  ELSE  0  THEN ;
[THEN]
Bernd Paysan's avatar
Bernd Paysan committed
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
[IFUNDEF] >stack
    : $@len ( $addr -- u ) \ gforth-string string-fetch-len
	\G returns the length of the stored string.
	@ dup IF  @  THEN ;
    : >pow2 ( n -- pow2 )
	1-
	dup 2/ or \ next power of 2
	dup 2 rshift or
	dup 4 rshift or
	dup 8 rshift or
	dup #16 rshift or
	[ cell 8 = [IF] ]
	    dup #32 rshift or
	    [ [THEN] ] 1+ ;
    : $padding ( n -- n' ) \ gforth-string
	[ 6 cells ] Literal +  >pow2  [ -4 cells ] Literal and ;
    : $!len ( u $addr -- ) \ gforth-string string-store-len
	\G changes the length of the stored string.  Therefore we must
	\G change the memory area and adjust address and count cell as
	\G well.
	over $padding  over @ IF  \ fast path for unneeded size change
	    over @ @ $padding over = IF  drop @ !  EXIT  THEN
	THEN
	over @ swap resize throw over ! @ ! ;
    : >stack ( x stack -- )
	\G push to top of stack
	>r r@ $@len cell+ r@ $!len
Bernd Paysan's avatar
Bernd Paysan committed
247
	r> $@ + 1 cells - ! ;
Bernd Paysan's avatar
Bernd Paysan committed
248
[THEN]
249 250 251

>CROSS

jwilke's avatar
jwilke committed
252
hex     \ the defualt base for the cross-compiler is hex !!
jwilke's avatar
jwilke committed
253
\ Warnings off
jwilke's avatar
jwilke committed
254 255 256

\ words that are generaly useful

257
: KB  400 * ;
jwilke's avatar
jwilke committed
258 259 260 261 262
: >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
263

pazsan's avatar
pazsan committed
264 265 266
: string, ( c-addr u -- )
    \ puts down string as cstring
    dup c, here swap chars dup allot move ;
267

268
: ," '"' parse string, ;
jwilke's avatar
jwilke committed
269

jwilke's avatar
jwilke committed
270
: SetValue ( n -- <name> )
pazsan's avatar
pazsan committed
271
\G Same behaviour as "Value" if the <name> is not defined
272
\G Same behaviour as "to" if the <name> is defined
jwilke's avatar
jwilke committed
273
\G SetValue searches in the current vocabulary
274
  save-input bl-word >r restore-input throw r> count
jwilke's avatar
jwilke committed
275 276 277 278 279
  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
280
	set-order
jwilke's avatar
jwilke committed
281
  ELSE Value THEN ;
jwilke's avatar
jwilke committed
282 283

: DefaultValue ( n -- <name> )
pazsan's avatar
pazsan committed
284 285
\G Same behaviour as "Value" if the <name> is not defined
\G DefaultValue searches in the current vocabulary
286
 save-input bl-word >r restore-input throw r> count
jwilke's avatar
jwilke committed
287
 get-current search-wordlist
288
 IF bl-word drop 2drop ELSE Value THEN ;
anton's avatar
anton committed
289 290 291

hex

jwilke's avatar
jwilke committed
292
\ FIXME delete`
jwilke's avatar
jwilke committed
293 294 295
\ 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
296

jwilke's avatar
jwilke committed
297
\ FIXME move down
jwilke's avatar
jwilke committed
298
: comment? ( c-addr u -- c-addr u )
299
        2dup s" (" str=
jwilke's avatar
jwilke committed
300
        IF    postpone (
301
        ELSE  2dup s" \" str= IF postpone \ THEN
jwilke's avatar
jwilke committed
302
        THEN ;
pazsan's avatar
pazsan committed
303

jwilke's avatar
jwilke committed
304 305 306 307
: X ( -- <name> )
\G The next word in the input is a target word.
\G Equivalent to T <name> but without permanent
\G switch to target dictionary. Used as prefix e.g. for @, !, here etc.
308
  bl-word count [ ' target >wordlist ] Literal search-wordlist
jwilke's avatar
jwilke committed
309 310 311
  IF state @ IF compile, ELSE execute THEN
  ELSE	-1 ABORT" Cross: access method not supported!"
  THEN ; immediate
312

jwilke's avatar
jwilke committed
313
\ Begin CROSS COMPILER:
anton's avatar
anton committed
314

315
\ debugging
jwilke's avatar
jwilke committed
316

317 318 319 320 321 322 323 324 325
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
326 327
>CROSS

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
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
347
  bl-word count debug? ;
348 349 350 351

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

jwilke's avatar
jwilke committed
358 359 360 361 362 363 364 365 366 367 368 369
: symentry ( adr len taddr -- )
\G Produce a symbol table (an optional symbol address
\G map) if wanted
    [ [IFDEF] fd-symbol-table ]
      base @ swap hex s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
      s" :" fd-symbol-table write-file throw
      fd-symbol-table write-line throw
    [ [ELSE] ]
      2drop drop
    [ [THEN] ] ;


jwilke's avatar
jwilke committed
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
\ \ --------------------	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:
415
\ if there is not enough space for the search path increase it!
jwilke's avatar
jwilke committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438


\ -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 )
439
    2dup c@ dup >r  + over c!  r> char+ +  swap move ;
jwilke's avatar
jwilke committed
440 441 442 443
[THEN]

[IFUNDEF] place
: place ( c-addr1 u c-addr2 )
444
    2dup c! char+ swap move ;
jwilke's avatar
jwilke committed
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
[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 |.
476
    name 2dup bounds ?DO i c@ '|' = IF 0 i c! THEN LOOP
jwilke's avatar
jwilke committed
477 478 479 480 481 482
    rot only-path ;

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

483
: path>string  cell+ dup cell+ swap @ ;
jwilke's avatar
jwilke committed
484 485 486 487 488 489 490 491

: 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^ -- )
492
  dup path>string
jwilke's avatar
jwilke committed
493 494 495 496
  BEGIN tuck dup WHILE repeat ;

: .path ( path-addr -- ) \ gforth
    \G Display the contents of the search path @var{path-addr}.
497
    path>string
jwilke's avatar
jwilke committed
498 499 500 501 502 503 504 505 506 507 508
    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!
509 510 511
    2dup 2 u> swap 1+ c@ ':' = and >r \ dos absoulte: c:/....
    over c@ '/' = >r
    over c@ '~' = >r
512 513
    \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
    S" ./" string-prefix?
jwilke's avatar
jwilke committed
514 515 516 517 518
    r> r> r> or or or ;

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

519
: pathsep? dup '/' = swap '\' = or ;
jwilke's avatar
jwilke committed
520 521 522 523 524 525 526 527 528

: 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~+ ( -- )
529
    ofile count s" ~+/" string-prefix?
jwilke's avatar
jwilke committed
530 531 532 533 534 535
    IF
	ofile count 3 /string ofile place
    THEN ;

: expandtopic ( -- ) \ stack effect correct? - anton
    \ expands "./" into an absolute name
536
    ofile count s" ./" string-prefix?
jwilke's avatar
jwilke committed
537 538 539 540 541 542 543 544 545
    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
546 547 548
    \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
    over swap
    BEGIN  dup  WHILE
549
        dup >r '/ scan 2dup s" /../" string-prefix?
pazsan's avatar
pazsan committed
550 551 552 553 554 555 556
        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
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
582
  ELSE  r> path>string
jwilke's avatar
jwilke committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        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

Bernd Paysan's avatar
Bernd Paysan committed
602 603 604 605
[IFDEF] add-included-file
    ' add-included-file alias h-add-included-file
[THEN]

jwilke's avatar
jwilke committed
606 607 608 609 610 611 612 613 614 615
>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
616
  WHILE	>r 2dup r@ >fl-name count str=
jwilke's avatar
jwilke committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	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
632 633
  add-included-file to source-desc
  sourcefilename ['] included catch
jwilke's avatar
jwilke committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  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 ;

650
: include bl-word count included ;
jwilke's avatar
jwilke committed
651

652
: require bl-word count required ;
jwilke's avatar
jwilke committed
653

jwilke's avatar
jwilke committed
654 655
0 [IF]

jwilke's avatar
jwilke committed
656 657 658 659 660 661 662 663 664 665
also forth definitions previous

: included ( adr len -- ) included ;

: required ( adr len -- ) required ;

: include include ;

: require require ;

jwilke's avatar
jwilke committed
666 667
[THEN]

jwilke's avatar
jwilke committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
>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
701
\ : defempty? .sourcepos ; 
jwilke's avatar
jwilke committed
702 703
[THEN]

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
\ \ --------------------        Compiler Plug Ins               01aug97jaw

>CROSS

\ Compiler States

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

: compiling? comp-state @ compiling = ;

: pi-undefined -1 ABORT" Plugin undefined" ;

720 721
[IFDEF] value! ' value! alias value-to [THEN]

722 723 724 725 726 727 728 729 730
: Plugin ( -- : pluginname )
  Create 
  \ for normal cross-compiling only one action
  \ exists, this fields are identical. For the instant
  \ simulation environment we need, two actions for each plugin
  \ the target one and the one that generates the simulation code
  ['] pi-undefined , \ action
  ['] pi-undefined , \ target plugin action
  8765 ,     \ plugin magic
731
[IFDEF] set-to
732
  ['] value-to set-to
733
[THEN]
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
  DOES> perform ;

Plugin DummyPlugin

: 'PI ( -- addr : pluginname )
  ' >body dup 2 cells + @ 8765 <> ABORT" not a plugin" ;

: plugin-of ( xt -- : pluginname )
  dup 'PI 2! ;

: action-of ( xt -- : plunginname )
  'PI cell+ ! ;

: TPA ( -- : plugin )
\ target plugin action
\ executes current target action of plugin
  'PI cell+ POSTPONE literal POSTPONE perform ; immediate

Variable ppi-temp 0 ppi-temp !

: pa:
\g define plugin action
  ppi-temp @ ABORT" pa: definition not closed"
  'PI ppi-temp ! :noname ;

: ;pa
\g end a definition for plugin action
  POSTPONE ; ppi-temp @ ! 0 ppi-temp ! ; immediate


jwilke's avatar
jwilke committed
764
Plugin dlit, ( d -- )			\ compile numerical value the target
765 766 767 768 769 770 771
Plugin lit, ( n -- )
Plugin alit, ( n -- )

Plugin branch, ( target-addr -- )	\ compiles a branch
Plugin ?branch, ( target-addr -- )	\ compiles a ?branch
Plugin branchmark, ( -- branch-addr )	\ reserves room for a branch
Plugin ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
772
Plugin ?dup-?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
Plugin ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
Plugin branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
' NOOP plugin-of branchto, 
Plugin branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
Plugin branchtomark, ( -- target-addr )	\ marks a branch destination

Plugin colon, ( tcfa -- )		\ compiles call to tcfa at current position
Plugin prim, ( tcfa -- )		\ compiles primitive invocation
Plugin colonmark, ( -- addr )		\ marks a colon call
Plugin colon-resolve ( tcfa addr -- )

Plugin addr-resolve ( target-addr addr -- )
Plugin doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )

Plugin ncontrols? ( [ xn ... x1 ] n -- ) \ checks wheter n control structures are open
Plugin if, 	( -- if-token )
789
Plugin ?dup-if, 	( -- if-token )
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
Plugin else,	( if-token -- if-token )
Plugin then,	( if-token -- )
Plugin ahead,
Plugin begin,
Plugin while,
Plugin until,
Plugin again,
Plugin repeat,
Plugin cs-swap	( x1 x2 -- x2 x1 )

Plugin case,	( -- n )
Plugin of,	( n -- x1 n )
Plugin endof,	( x1 n -- x2 n )
Plugin endcase,	( x1 .. xn n -- )

Plugin do,	( -- do-token )
Plugin ?do,	( -- ?do-token )
807 808
Plugin +do,	( -- ?do-token )
Plugin -do,	( -- ?do-token )
809
Plugin u-do,	( -- ?do-token )
810 811 812
Plugin for,	( -- for-token )
Plugin loop,	( do-token / ?do-token -- )
Plugin +loop,	( do-token / ?do-token -- )
pazsan's avatar
pazsan committed
813
Plugin -loop,	( do-token / ?do-token -- )
814 815 816 817
Plugin next,	( for-token )
Plugin leave,	( -- )
Plugin ?leave, 	( -- )

jwilke's avatar
jwilke committed
818 819 820 821 822 823 824 825
Plugin ca>native  \ Convert a code address to the processors
                  \ native address. This is used in doprim, and
                  \ code/code: primitive definitions word to
                  \ convert the addresses.
                  \ The only target where we need this is the misc
                  \ which is a 16 Bit processor with word addresses
                  \ but the forth system we build has a normal byte
                  \ addressed memory model    
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

Plugin doprim,	\ compiles start of a primitive
Plugin docol,   	\ compiles start of a colon definition
Plugin doer,		
Plugin fini,      \ compiles end of definition ;s
Plugin doeshandler,
Plugin dodoes,

Plugin colon-start
' noop plugin-of colon-start
Plugin colon-end
' noop plugin-of colon-end

Plugin ]comp     \ starts compilation
' noop plugin-of ]comp
Plugin comp[     \ ends compilation
' noop plugin-of comp[

Plugin t>body             \ we need the system >body
			\ and the target >body

>TARGET
: >body t>body ;


851
\ Ghost Builder                                        06oct92py
jwilke's avatar
jwilke committed
852

853
>CROSS
854
hex
855
\ Values for ghost magic
856 857 858
4711 Constant <fwd>             4712 Constant <res>
4713 Constant <imm>             4714 Constant <do:>
4715 Constant <skip>
jwilke's avatar
jwilke committed
859

860
\ Bitmask for ghost flags
861
1 Constant <unique>
862 863 864 865 866 867 868 869 870 871 872 873
2 Constant <primitive>

\ FXIME: move this to general stuff?
: set-flag ( addr flag -- )
  over @ or swap ! ;

: reset-flag ( addr flag -- )
  invert over @ and swap ! ;

: get-flag ( addr flag -- f )
  swap @ and 0<> ;
  
jwilke's avatar
jwilke committed
874

875
Struct
jwilke's avatar
jwilke committed
876

877 878
  \ link to next ghost (always the first element)
  cell% field >next-ghost
jwilke's avatar
jwilke committed
879

880 881 882 883 884 885
  \ type of ghost
  cell% field >magic
		
  \ pointer where ghost is in target, or if unresolved
  \ points to the where we have to resolve (linked-list)
  cell% field >link
jwilke's avatar
jwilke committed
886

887
  \ execution semantics (while target compiling) of ghost
888
  cell% field >exec
jwilke's avatar
jwilke committed
889

890 891 892 893
  \ compilation action of this ghost; this is what is
  \ done to compile a call (or whatever) to this definition.
  \ E.g. >comp contains the semantic of postpone s"
  \ whereas >exec-compile contains the semantic of s"
894 895
  cell% field >comp

896 897 898 899 900
  \ Compilation sematics (while parsing) of this ghost. E.g. 
  \ "\" will skip the rest of line.
  \ These semantics are defined by Cond: and
  \ if a word is made immediate in instant, then the >exec2 field
  \ gets copied to here
901
  cell% field >exec-compile
jwilke's avatar
jwilke committed
902

903 904 905
  \ Additional execution semantics of this ghost. This is used
  \ for code generated by instant and for the doer-xt of created
  \ words
906 907 908
  cell% field >exec2

  cell% field >created
jwilke's avatar
jwilke committed
909

910 911
  \ the xt of the created ghost word itself
  cell% field >ghost-xt
jwilke's avatar
jwilke committed
912

913 914 915
  \ pointer to the counted string of the assiciated
  \ assembler label
  cell% field >asm-name
916

917 918 919 920 921 922 923 924
  \ mapped primitives have a special address, so
  \ we are able to detect them
  cell% field >asm-dummyaddr
			
  \ for builder (create, variable...) words
  \ the execution symantics of words built are placed here
  \ this is a doer ghost or a dummy ghost
  cell% field >do:ghost
925

926
  cell% field >ghost-flags
jwilke's avatar
jwilke committed
927

928 929
  cell% field >ghost-name

930 931
\  cell% field >ghost-vt

932 933 934 935
End-Struct ghost-struct

Variable ghost-list
0 ghost-list !
jwilke's avatar
jwilke committed
936 937

Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
938
\ Variable last-ghost	\ last ghost that is created
jwilke's avatar
jwilke committed
939 940
Variable last-header-ghost \ last ghost definitions with header

941
\ space for ghosts resolve structure
942
\ we create ghosts in a separate space
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
\ and not to the current host dp, because this
\ gives trouble with instant while compiling and creating
\ a ghost for a forward reference
\ BTW: we cannot allocate another memory region
\ because allot will check the overflow!!
Variable cross-space-dp
Create cross-space 250000 allot here 100 allot align 
Constant cross-space-end
cross-space cross-space-dp !
Variable cross-space-dp-orig

: cross-space-used cross-space-dp @ cross-space - ;

: >space ( -- )
  dp @ cross-space-dp-orig !
  cross-space-dp @ dp ! ;

: space> ( -- )
  dp @ dup cross-space-dp !
  cross-space-end u> ABORT" CROSS: cross-space overflow"
  cross-space-dp-orig @ dp ! ;

965
\ this is just for debugging, to see this in the backtrace
966 967 968 969 970 971 972 973 974 975 976
: execute-exec execute ;
: execute-exec2 execute ;
: execute-exec-compile execute ;

: NoExec
  executed-ghost @ >exec2 @
  ?dup 
  IF   execute-exec2
  ELSE true ABORT" CROSS: Don't execute ghost, or immediate target word"
  THEN ;

977 978
Defer is-forward

979
: (ghostheader) ( -- )
jwilke's avatar
jwilke committed
980
    ghost-list linked <fwd> , 0 , ['] NoExec , ['] is-forward ,
981
    0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ;
982 983 984 985 986 987 988 989

: ghostheader ( -- ) (ghostheader) 0 , ;

' Ghosts >wordlist Constant ghosts-wordlist

\ the current wordlist for ghost definitions in the host
ghosts-wordlist Value current-ghosts

jwilke's avatar
jwilke committed
990
: Make-Ghost ( "name" -- ghost )
991 992 993 994 995 996 997 998 999
  >space
  \ save current and create in ghost vocabulary
  get-current >r current-ghosts set-current
  >in @ Create >in !
  \ some forth systems like iForth need the immediate directly
  \ after the word is created
  \ restore current
  r> set-current
  here (ghostheader)
1000
  bl-word count string, align
1001 1002 1003 1004 1005 1006 1007 1008 1009
  space>
  \ set ghost-xt field by doing a search
  dup >ghost-name count 
  current-ghosts search-wordlist
  0= ABORT" CROSS: Just created, must be there!"
  over >ghost-xt !
  DOES> 
      dup executed-ghost !
      >exec @ execute-exec ;
jwilke's avatar
jwilke committed
1010 1011 1012 1013

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

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
Defer search-ghosts

: (search-ghosts) ( adr len -- cfa true | 0 )
  current-ghosts search-wordlist ; 

  ' (search-ghosts) IS search-ghosts

: gsearch ( addr len -- ghost true | 0 )
  search-ghosts
  dup IF swap >body swap THEN ;

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
Variable cross-wheres

0
cell +field gwhere-nt
cell +field gwhere-loc
constant gwhere-struct

: tsourceview ( -- view )
    [IFDEF] loadfilename# loadfilename# @
    [ELSE] sourcefilename str>loadfilename# [THEN]
    sourceline#
    input-lexeme 2@ drop source drop -
    $ff min swap 8 lshift + $7fffff min swap #23 lshift or ;

: gwhere-duplicate? ( ghost -- flag )
    cross-wheres $@ dup if
	gwhere-struct - + >r
	dup r@ gwhere-nt @ =
	r> gwhere-loc @ tsourceview = and if
	    drop true exit then
    else
	2drop then
    drop false ;

1049 1050 1051 1052 1053
[IFUNDEF] $+!len
    : $+!len ( n $addr -- addr )
	>r r@ $@len tuck + r@ $!len r> @ cell+ + ;
[THEN]

1054 1055 1056 1057 1058 1059 1060
: gwhere, ( ghost -- )
    dup gwhere-duplicate? 0= IF
	gwhere-struct cross-wheres $+!len >r
	dup r@ gwhere-nt !
	tsourceview r> gwhere-loc !
    THEN  drop ;

1061 1062 1063 1064 1065
\ locations

0 Value glocs-start
Variable cross-locs[]

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
[IFUNDEF] $[]
: $room ( u $addr -- )
    \G generate room for at least u bytes, erase when expanding
    >r dup r@ $@len tuck u<= IF  rdrop 2drop EXIT  THEN
    - dup r> $+!len swap 0 fill ;

: $[] ( u $[]addr -- addr' )
    \G index into the string array and return the address at index @var{u}
    \G The array will be resized as needed
    >r cells dup cell+ r@ $room  r> $@ drop + ;
[THEN]

1078 1079 1080 1081 1082
: tsourcepos1 ( -- xpos )
    [IFDEF] replace-sourceview
	replace-sourceview  0 to replace-sourceview ?dup ?EXIT
    [THEN]
    tsourceview ;
1083 1084 1085
: gxt-location ( addr -- addr )
\ note that an xt was compiled at addr, for backtrace-locate functionality
    dup glocs-start - T 1 cells H / >r
1086
    tsourcepos1 dup r> 1+ cross-locs[] $[] 1 cells - 2! ;
1087 1088 1089

\ search for ghosts

1090
: gfind   ( string -- ghost true / string false )
jwilke's avatar
jwilke committed
1091
\ searches for string in word-list ghosts
1092 1093
  \ dup count type space
  dup >r count gsearch
1094
  dup IF rdrop over gwhere, ELSE r> swap THEN ;
jwilke's avatar
jwilke committed
1095 1096

: gdiscover ( xt -- ghost true | xt false )
1097
  >r ghost-list
jwilke's avatar
jwilke committed
1098
  BEGIN @ dup
1099 1100 1101 1102
  WHILE dup >magic @ <fwd> <>
        IF dup >link @ r@ =
           IF rdrop true EXIT THEN
        THEN
jwilke's avatar
jwilke committed
1103
  REPEAT
1104
  drop r> false ;
jwilke's avatar
jwilke committed
1105

1106 1107 1108
: xt>ghost ( xt -- ghost )
  gdiscover 0= ABORT" CROSS: ghost not found for this xt" ;

1109
: Ghost   ( "name" -- ghost )
1110
  >in @ bl-word gfind IF  nip EXIT  THEN
jwilke's avatar
jwilke committed
1111 1112 1113
  drop  >in !  Make-Ghost ;

: >ghostname ( ghost -- adr len )
1114
  >ghost-name count ;
jwilke's avatar
jwilke committed
1115 1116 1117 1118

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

1119 1120 1121
: undefined? ( ghost -- flag )
  >magic @ dup <fwd> = swap <skip> = or ;

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
: immediate? ( ghost -- flag )
  >magic @ <imm> = ;

Variable TWarnings
TWarnings on
Variable Exists-Warnings
Exists-Warnings on

: exists-warning ( ghost -- ghost )
  TWarnings @ Exists-Warnings @ and
  IF dup >ghostname warnhead type ."  exists " THEN ;

\ : HeaderGhost Ghost ;

Variable reuse-ghosts reuse-ghosts off

: HeaderGhost ( "name" -- ghost )
  >in @ 
1140
  bl-word count 
1141 1142 1143 1144 1145 1146 1147 1148 1149
\  2dup type space
  current-ghosts search-wordlist
  IF  >body dup undefined? reuse-ghosts @ or
      IF   nip EXIT
      ELSE exists-warning 
      THEN
      drop >in ! 
  ELSE >in ! 
  THEN 
1150
  \ we keep the execution semantics of the previously
1151 1152 1153 1154 1155 1156 1157 1158
  \ defined words, this is a workaround
  \ for the redefined \ until vocs work
  Make-Ghost ;
 
: .ghost ( ghost -- ) >ghostname type ;

\ ' >ghostname ALIAS @name

1159
: findghost ( "ghostname" -- ghost ) 
1160
  bl-word gfind 0= ABORT" CROSS: Ghost don't exists" ;
1161

1162 1163
: [G'] ( -- ghost : name )
\G ticks a ghost and returns its address
1164 1165
  findghost
  state @ IF postpone literal THEN ; immediate
1166

1167 1168
: g>xt ( ghost -- xt )
\G Returns the xt (cfa) of a ghost. Issues a warning if undefined.
1169 1170
  dup undefined? ABORT" CROSS: forward " >link @ ;
   
1171 1172 1173 1174 1175
: g>body ( ghost -- body )
\G Returns the body-address (pfa) of a ghost. 
\G Issues a warning if undefined (a forward-reference).
  g>xt X >body ;

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
1 Constant <label>

Struct
  \ bitmask of address type (not used for now)
  cell% field addr-type
  \ if this address is an xt, this field points to the ghost
  cell% field addr-xt-ghost
  \ a bit mask that tells as what part of the cell
  \ is refenced from an address pointer, used for assembler generation
  cell% field addr-refs
End-Struct addr-struct

: %allocerase ( align size -- addr )
  dup >r %alloc dup r> erase ;

\ returns the addr struct, define it if 0 reference
: define-addr-struct ( addr -- struct-addr )
  dup @ ?dup IF nip EXIT THEN
  addr-struct %allocerase tuck swap ! ;

1196 1197
>cross

jwilke's avatar
jwilke committed
1198 1199
\ Predefined ghosts                                    12dec92py

jwilke's avatar
jwilke committed
1200 1201
Ghost - drop \ need a ghost otherwise "-" would be treated as a number

1202 1203
Ghost 0=                                        drop
Ghost branch    Ghost ?branch                   2drop
1204
Ghost ?dup-?branch drop
1205
Ghost unloop    Ghost ;S                        2drop
1206 1207
Ghost lit       Ghost !                         2drop
Ghost noop                                      drop
1208 1209 1210
Ghost over      Ghost =         Ghost drop      2drop drop
Ghost 2drop drop
Ghost 2dup drop
1211 1212 1213
Ghost call drop
Ghost @ drop
Ghost useraddr drop
Bernd Paysan's avatar
Bernd Paysan committed
1214
Ghost user@ drop
1215 1216 1217 1218
Ghost execute drop
Ghost + drop
Ghost decimal drop
Ghost hex drop
1219 1220 1221
Ghost lit@ drop
Ghost lit-perform drop
Ghost lit+ drop
1222
Ghost does-xt drop
1223
Ghost no-to drop
1224
Ghost refill drop
jwilke's avatar
jwilke committed
1225

1226
Ghost :docol    Ghost :dodoes   2drop
Bernd Paysan's avatar
Bernd Paysan committed
1227
Ghost :dovar	drop
1228

jwilke's avatar
jwilke committed
1229 1230
\ \ Parameter for target systems                         06oct92py

1231

jwilke's avatar
jwilke committed
1232 1233 1234
\ we define it ans like...
wordlist Constant target-environment

1235 1236
\ save information of current dictionary to restore with environ>
Variable env-current 
jwilke's avatar
jwilke committed
1237 1238 1239 1240 1241 1242

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

>TARGET

1243 1244 1245 1246 1247
: environment? ( addr len -- [ x ] true | false )
\G returns the content of environment variable and true or
\G false if not present
   target-environment search-wordlist 
   IF EXECUTE true ELSE false THEN ;
jwilke's avatar
jwilke committed
1248

1249 1250 1251 1252
: $has? ( addr len -- x | false )
\G returns the content of environment variable 
\G or false if not present
   T environment? H dup IF drop THEN ;
jwilke's avatar
jwilke committed
1253

1254 1255 1256
: e? ( "name" -- x )
\G returns the content of environment variable. 
\G The variable is expected to exist. If not, issue an error.
1257
   bl-word count T environment? H 
1258 1259 1260 1261 1262
   0= ABORT" environment variable not defined!" ;

: has? ( "name" --- x | false )
\G returns the content of environment variable 
\G or false if not present
1263
   bl-word count T $has? H ;
jwilke's avatar
jwilke committed
1264 1265


pazsan's avatar
pazsan committed
1266 1267
>ENVIRON get-order get-current swap 1+ set-order
true SetValue compiler
1268
true SetValue cross
pazsan's avatar
pazsan committed
1269 1270
true SetValue standard-threading
>TARGET previous
jwilke's avatar
jwilke committed
1271

jwilke's avatar
jwilke committed
1272
0
1273 1274 1275
[IFDEF] mach-file drop mach-file count 1 [THEN]
[IFDEF] machine-file drop machine-file 1 [THEN]
[IF] 	included hex
jwilke's avatar
jwilke committed
1276 1277
[ELSE]  cr ." No machine description!" ABORT 
[THEN]
jwilke's avatar
jwilke committed
1278

jwilke's avatar
jwilke committed
1279 1280
>ENVIRON

pazsan's avatar
pazsan committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
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
1293 1294 1295
false DefaultValue backtrace
false DefaultValue new-input
false DefaultValue peephole
1296
false DefaultValue primcentric
1297
false DefaultValue abranch
1298
true DefaultValue f83headerstring
pazsan's avatar
pazsan committed
1299
true DefaultValue control-rack
pazsan's avatar
pazsan committed
1300 1301
[THEN]

1302
true DefaultValue gforthcross
pazsan's avatar
pazsan committed
1303 1304 1305
true DefaultValue interpreter
true DefaultValue ITC
false DefaultValue rom
pazsan's avatar
pazsan committed
1306
false DefaultValue flash
1307
true DefaultValue standardthreading
jwilke's avatar
jwilke committed
1308

1309 1310 1311 1312 1313
\ ANSForth environment  stuff
8 DefaultValue ADDRESS-UNIT-BITS
255 DefaultValue MAX-CHAR
255 DefaultValue /COUNTED-STRING

jwilke's avatar
jwilke committed
1314 1315
>TARGET
s" relocate" T environment? H 
1316 1317
\ JAW why set NIL to this?!
[IF]	drop \ SetValue NIL
1318
[ELSE]	>ENVIRON X NIL SetValue relocate
jwilke's avatar
jwilke committed
1319
[THEN]
1320 1321 1322
>TARGET

0 Constant NIL
pazsan's avatar
pazsan committed
1323 1324 1325

>CROSS

jwilke's avatar
jwilke committed
1326
\ \ Create additional parameters                         19jan95py
1327

1328
\ currently cross only works for host machines with address-unit-bits
1329
\ equal to 8 because of s! and sc!
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
\ 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
1345
\ this sets byte size for the target machine, (probably right guess) jaw
jwilke's avatar
jwilke committed
1346

1347
T
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
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]
1362
H
1363
tbits/char bits/byte /	Constant tbyte
1364

1365 1366 1367
: >signed ( u -- n )
    1 tbits/char tcell * 1- lshift 2dup and
    IF  negate or  ELSE  drop  THEN ;
1368

anton's avatar
anton committed
1369 1370
\ Variables                                            06oct92py

1371 1372
Variable (tlast)    
(tlast) Value tlast TNIL tlast !  \ Last name field
anton's avatar
anton committed
1373 1374
Variable tlastcfa \ Last code field

jwilke's avatar
jwilke committed
1375 1376 1377 1378 1379
\ statistics						10jun97jaw

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

jwilke's avatar
jwilke committed
1380 1381 1382
: target>bitmask-size ( u1 -- u2 )
  1- tcell>bit rshift 1+ ;

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

pazsan's avatar
pazsan committed
1387
\ \ memregion.fs
jwilke's avatar
jwilke committed
1388 1389 1390 1391 1392 1393 1394


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 !

1395 1396
: >rname 9 cells + ;
: >rtouch 8 cells + ; \ executed when region is accessed
1397
: >rbm   4 cells + ; \ bitfield per cell witch indicates relocation
1398
: >rmem  5 cells + ;
1399 1400
: >rtype 6 cells + ; \ field per cell witch points to a type struct
: >rrom 7 cells + ;  \ a -1 indicates that this region is rom
jwilke's avatar
jwilke committed
1401
: >rlink 3 cells + ;
jwilke's avatar
jwilke committed
1402 1403 1404 1405
: >rdp 2 cells + ;
: >rlen cell+ ;
: >rstart ;

1406 1407 1408 1409
: (region) ( addr len region -- )
\G change startaddress and length of an existing region
  >r r@ last-defined-region !
  r@ >rlen ! dup r@ >rstart ! r> >rdp ! ;
jwilke's avatar
jwilke committed
1410

1411 1412
: uninitialized -1 ABORT" CROSS: Region is uninitialized" ;

jwilke's avatar
jwilke committed
1413
: region ( addr len -- "name" )                
1414
\G create a new region
jwilke's avatar
jwilke committed
1415
  \ check whether predefined region exists 
1416
  save-input bl-word find >r >r restore-input throw r> r> 0= 
jwilke's avatar
jwilke committed
1417 1418 1419 1420 1421
  IF	\ make region
	drop
	save-input create restore-input throw
	here last-defined-region !
	over ( startaddr ) , ( length ) , ( dp ) ,
1422 1423
	region-link linked 0 , 0 , 0 , 0 , 
        ['] uninitialized ,
1424
        bl-word count string,
jwilke's avatar
jwilke committed
1425
  ELSE	\ store new parameters in region
1426
        bl-word drop
1427
	>body (region)
jwilke's avatar
jwilke committed
1428 1429
  THEN ;

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

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

1438 1439
: area ( region -- startaddr totallen ) 
\G returns the total area
1440
  dup >rstart @ swap >rlen @ ;
jwilke's avatar
jwilke committed
1441

1442 1443 1444
: dp@ ( region -- dp )
  >rdp @ ;

1445 1446
: mirrored ( -- )                              
\G mark last defined region as mirrored
jwilke's avatar
jwilke committed
1447
  mirrored-link
jwilke's avatar
jwilke committed
1448
  align linked last-defined-region @ , ;
jwilke's avatar
jwilke committed
1449

1450 1451 1452 1453
: writeprotected
\G mark a region as write protected
  -1 last-defined-region @ >rrom ! ;

jwilke's avatar
jwilke committed
1454 1455
: .addr ( u -- )
\G prints a 16 or 32 Bit nice hex value
jwilke's avatar
jwilke committed
1456 1457
  base @ >r hex
  tcell 2 u>
1458
  IF s>d <# # # # # '.' hold # # # # #> type
jwilke's avatar
jwilke committed
1459
  ELSE s>d <# # # # # # #> type
1460
  THEN r> base ! space ;
jwilke's avatar
jwilke committed
1461 1462 1463 1464 1465 1466 1467 1468

: .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
1469 1470 1471
  WHILE cr
        0 >rlink - >r
        r@ >rname count tuck type
Bernd Paysan's avatar
Bernd Paysan committed
1472
        12 swap - spaces space
jwilke's avatar
jwilke committed
1473 1474 1475
        ." Start: " r@ >rstart @ dup .addr space
        ." End: " r@ >rlen @ + .addr space
        ." DP: " r> >rdp @ .addr
jwilke's avatar
jwilke committed
1476
  REPEAT drop
jwilke's avatar
jwilke committed
1477
  s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
1478 1479 1480
  cr ." Mirrored:"
  mirrored-link @
  BEGIN dup
jwilke's avatar
jwilke committed
1481
  WHILE space dup cell+ @ >rname count type @
jwilke's avatar
jwilke committed
1482 1483 1484 1485 1486 1487 1488 1489
  REPEAT drop cr
  ;

\ -------- predefined regions

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

1490 1491 1492 1493
0 0 region user-region
\ data for user variables goes here
\ this has to be defined before dictionary or ram-dictionary

jwilke's avatar
jwilke committed
1494 1495 1496
0 0 region dictionary
\ rom area for the compiler

jwilke's avatar
jwilke committed
1497
T has? rom H
jwilke's avatar
jwilke committed
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
[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

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
: setup-region ( region -- )
  >r
  \ allocate mem
  r@ >rlen @ allocatetarget
  r@ >rmem !

  r@ >rlen @
  target>bitmask-size allocatetarget
  r@ >rbm !

  r@ >rlen @
  tcell / 1+ cells allocatetarget r@ >rtype !

  ['] noop r@ >rtouch !
  rdrop ;
jwilke's avatar
jwilke committed
1528

1529
: setup-target ( -- )   \G initialize target's memory space
jwilke's avatar
jwilke committed
1530
  s" rom" T $has? H
jwilke's avatar
jwilke committed
1531
  IF  \ check for ram and rom...
1532
      \ address-space area nip 0<>
jwilke's avatar
jwilke committed
1533 1534
      ram-dictionary area nip 0<>
      rom-dictionary area nip 0<>
1535
      and 0=
jwilke's avatar
jwilke committed
1536 1537 1538 1539 1540 1541 1542 1543
      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
1544
  nip 0=
jwilke's avatar
jwilke committed
1545
  ABORT" CROSS: define at least address-space or dictionary!!"
jwilke's avatar
jwilke committed
1546 1547 1548 1549 1550 1551 1552

  \ allocate target for each region
  region-link
  BEGIN @ dup
  WHILE dup
        0 >rlink - >r
        r@ >rlen @
1553 1554
        IF      r@ setup-region
        THEN    rdrop
1555 1556
   REPEAT drop ;

1557
\ MakeKernel                                           		22feb99jaw
1558

1559
: makekernel ( start targetsize -- )
1560 1561
\G convenience word to setup the memory of the target
\G used by main.fs of the c-engine based systems
1562
  dictionary (region) setup-target ;
1563 1564 1565 1566

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

pazsan's avatar
pazsan committed
1568
\ \ switched tdp for rom support				03jun97jaw
jwilke's avatar
jwilke committed
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

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

jwilke's avatar
jwilke committed
1587 1588 1589
0 Value current-region
0 Value tdp
Variable fixed		\ flag: true: no automatic switching
jwilke's avatar
jwilke committed
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
			\	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
1602 1603
: activate ( region -- )
\G next code goes to this region
jwilke's avatar
jwilke committed
1604
  dup to current-region >rdp to tdp ;
jwilke's avatar
jwilke committed
1605

jwilke's avatar
jwilke committed
1606
: (switchram)
jwilke's avatar
jwilke committed
1607
  fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
jwilke's avatar
jwilke committed
1608
  ram-dictionary activate ;
jwilke's avatar
jwilke committed
1609 1610 1611 1612 1613 1614

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

: switchrom
jwilke's avatar
jwilke committed
1615
  fixed @ ?EXIT rom-dictionary activate ;
jwilke's avatar
jwilke committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630

: >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
1631
\ default compilation goes to rom
jwilke's avatar
jwilke committed
1632 1633 1634 1635
\ when romable support is off, only the rom switch is used (!!)
>auto

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

anton's avatar
anton committed
1637 1638
>TARGET

jwilke's avatar
jwilke committed
1639 1640
\ \ Target Memory Handling

anton's avatar
anton committed
1641 1642
\ Byte ordering and cell size                          06oct92py

1643 1644
: cell+         tcell + ;
: cells         tcell<< lshift ;
1645 1646
: chars         tchar * ;
: char+		tchar + ;
1647
: floats	tfloat * ;
1648
    
anton's avatar
anton committed
1649
>CROSS
1650
: cell/         tcell<< rshift ;
anton's avatar
anton committed
1651 1652
>TARGET
20 CONSTANT bl
jwilke's avatar
jwilke committed
1653
\ TNIL Constant NIL
anton's avatar
anton committed
1654 1655 1656

>CROSS

1657 1658
bigendian
[IF]
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    : DS!  ( d addr -- )
	tcell bounds swap 1-
	DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
    : DS@  ( addr -- d )
	>r 0 0 r> tcell bounds
	DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP ;
    : 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 ;
1671
[ELSE]
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
    : DS!  ( d addr -- )
	tcell bounds
	DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
    : DS@  ( addr -- n )
	>r 0 0 r> tcell bounds swap 1-
	DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP ;
    : 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
1684 1685
[THEN]

1686 1687 1688
: S! ( n addr -- ) >r s>d r> DS! ;
: S@ ( addr -- n ) DS@ d>s ;

jwilke's avatar
jwilke committed
1689 1690 1691 1692 1693 1694 1695 1696 1697
: 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
1698 1699 1700
                IF r> r> drop nip 
                   dup >rtouch @ EXECUTE EXIT 
                THEN
jwilke's avatar
jwilke committed
1701 1702 1703 1704 1705 1706
        THEN
        r> drop
        r>
  REPEAT
  2drop 0 ;

1707
: taddr>region-abort ( taddr -- region | 0 )
1708 1709
\G Same as taddr>region but aborts if taddr is not
\G a valid address in the target address space
1710 1711 1712 1713 1714
  dup taddr>region dup 0= 
  IF    drop cr ." Wrong address: " .addr
        -1 ABORT" Address out of range!"
  THEN nip ;

jwilke's avatar
jwilke committed
1715 1716 1717
: (>regionimage) ( taddr -- 'taddr )
  dup
  \ find region we want to address
1718
  taddr>region-abort
jwilke's avatar
jwilke committed
1719 1720 1721 1722 1723 1724
  >r
  \ calculate offset in region
  r@ >rstart @ -
  \ add regions real address in our memory
  r> >rmem @ + ;

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
: (>regionramimage) ( taddr -- 'taddr )
\G same as (>regionimage) but aborts if the region is rom
  dup
  \ find region we want to address
  taddr>region-abort
  >r
  r@ >rrom @ ABORT" CROSS: region is write-protected!"
  \ calculate offset in region
  r@ >rstart @ -
  \ add regions real address in our memory
  r> >rmem @ + ;

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
: (>regionbm) ( taddr -- 'taddr bitmaskbaseaddr )
  dup
  \ find region we want to address
  taddr>region-abort
  >r
  \ calculate offset in region
  r@ >rstart @ -
  \ add regions real address in our memory
  r> >rbm @ ;

: (>regiontype) ( taddr -- 'taddr )
  dup
  \ find region we want to address
  taddr>region-abort
  >r
  \ calculate offset in region
  r@ >rstart @ - tcell / cells
  \ add regions real address in our memory
  r> >rtype @ + ;
  
anton's avatar
anton committed
1757 1758 1759 1760 1761 1762 1763
\ 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
1764
: -bit ( addr n -- )  >bit invert over c@ and swap c! ;
jwilke's avatar
jwilke committed
1765

1766 1767
: @relbit ( taddr -- f ) (>regionbm) swap cell/ >bit swap c@ and ;

1768 1769 1770 1771 1772
: (relon) ( taddr -- )  
  [ [IFDEF] fd-relocation-table ]
  s" +" fd-relocation-table write-file throw
  dup s>d <# #s #> fd-relocation-table write-line throw
  [ [THEN] ]
1773
  (>regionbm) swap cell/ +bit ;
1774 1775 1776 1777 1778 1779

: (reloff) ( taddr -- ) 
  [ [IFDEF] fd-relocation-table ]
  s" -" fd-relocation-table write-file throw
  dup s>d <# #s #> fd-relocation-table write-line throw
  [ [THEN] ]
1780
  (>regionbm) swap cell/ -bit ;
jwilke's avatar
jwilke committed
1781 1782

DEFER >image
1783
DEFER >ramimage