Annotation of gforth/cross.fs, revision 1.77
1.1 anton 1: \ CROSS.FS The Cross-Compiler 06oct92py
2: \ Idea and implementation: Bernd Paysan (py)
1.30 anton 3:
1.69 anton 4: \ Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
1.30 anton 5:
6: \ This file is part of Gforth.
7:
8: \ Gforth is free software; you can redistribute it and/or
9: \ modify it under the terms of the GNU General Public License
10: \ as published by the Free Software Foundation; either version 2
11: \ of the License, or (at your option) any later version.
12:
13: \ This program is distributed in the hope that it will be useful,
14: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
15: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: \ GNU General Public License for more details.
17:
18: \ You should have received a copy of the GNU General Public License
19: \ along with this program; if not, write to the Free Software
20: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1.1 anton 21:
1.67 jwilke 22: 0
23: [IF]
24:
25: ToDo:
26: Crossdoc destination ./doc/crossdoc.fd makes no sense when
27: cross.fs is uses seperately. jaw
28: Do we need this char translation with >address and in branchoffset?
29: (>body also affected) jaw
30: Clean up mark> and >resolve stuff jaw
31:
32: [THEN]
1.48 anton 33:
1.77 ! jwilke 34: hex
! 35:
! 36: \ debugging for compiling
! 37:
! 38: \ print stack at each colon definition
! 39: \ : : save-input cr bl word count type restore-input throw .s : ;
! 40:
! 41: \ print stack at each created word
! 42: \ : create save-input cr bl word count type restore-input throw .s create ;
! 43:
! 44:
! 45: \ \ ------------- Setup Vocabularies
! 46:
! 47: \ Remark: Vocabulary is not ANS, but it should work...
! 48:
! 49: Vocabulary Cross
! 50: Vocabulary Target
! 51: Vocabulary Ghosts
! 52: Vocabulary Minimal
! 53: only Forth also Target also also
! 54: definitions Forth
! 55:
! 56: : T previous Cross also Target ; immediate
! 57: : G Ghosts ; immediate
! 58: : H previous Forth also Cross ; immediate
! 59:
! 60: forth definitions
! 61:
! 62: : T previous Cross also Target ; immediate
! 63: : G Ghosts ; immediate
! 64:
! 65: : >cross also Cross definitions previous ;
! 66: : >target also Target definitions previous ;
! 67: : >minimal also Minimal definitions previous ;
! 68:
! 69: H
! 70:
! 71: >CROSS
! 72:
! 73: \ find out whether we are compiling with gforth
! 74:
! 75: : defined? bl word find nip ;
! 76: defined? emit-file defined? toupper and \ drop 0
! 77: [IF]
! 78: \ use this in a gforth system
! 79: : \GFORTH ; immediate
! 80: : \ANSI postpone \ ; immediate
! 81: [ELSE]
! 82: : \GFORTH postpone \ ; immediate
! 83: : \ANSI ; immediate
! 84: [THEN]
! 85:
! 86: \ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
! 87: \ANSI : [IFDEF] defined? postpone [IF] ; immediate
! 88: 0 \ANSI drop 1
! 89: [IF]
! 90: : \G postpone \ ; immediate
! 91: : rdrop postpone r> postpone drop ; immediate
! 92: : name bl word count ;
! 93: : bounds over + swap ;
! 94: : scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
! 95: : linked here over @ , swap ! ;
! 96: : alias create , DOES> @ EXECUTE ;
! 97: : defer ['] noop alias ;
! 98: : is state @
! 99: IF ' >body postpone literal postpone !
! 100: ELSE ' >body ! THEN ; immediate
! 101: : 0>= 0< 0= ;
! 102: : d<> rot <> -rot <> or ;
! 103: : toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
! 104: Variable ebuf
! 105: : emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
! 106: 0a Constant #lf
! 107: 0d Constant #cr
! 108:
! 109: [IFUNDEF] Warnings Variable Warnings [THEN]
! 110:
! 111: \ \ Number parsing 23feb93py
! 112:
! 113: \ number? number 23feb93py
! 114:
! 115: Variable dpl
! 116:
! 117: hex
! 118: Create bases 10 , 2 , A , 100 ,
! 119: \ 16 2 10 character
! 120:
! 121: \ !! protect BASE saving wrapper against exceptions
! 122: : getbase ( addr u -- addr' u' )
! 123: over c@ [char] $ - dup 4 u<
! 124: IF
! 125: cells bases + @ base ! 1 /string
! 126: ELSE
! 127: drop
! 128: THEN ;
! 129:
! 130: : sign? ( addr u -- addr u flag )
! 131: over c@ [char] - = dup >r
! 132: IF
! 133: 1 /string
! 134: THEN
! 135: r> ;
! 136:
! 137: : s>unumber? ( addr u -- ud flag )
! 138: base @ >r dpl on getbase
! 139: 0. 2swap
! 140: BEGIN ( d addr len )
! 141: dup >r >number dup
! 142: WHILE \ there are characters left
! 143: dup r> -
! 144: WHILE \ the last >number parsed something
! 145: dup 1- dpl ! over c@ [char] . =
! 146: WHILE \ the current char is '.'
! 147: 1 /string
! 148: REPEAT THEN \ there are unparseable characters left
! 149: 2drop false
! 150: ELSE
! 151: rdrop 2drop true
! 152: THEN
! 153: r> base ! ;
! 154:
! 155: \ ouch, this is complicated; there must be a simpler way - anton
! 156: : s>number? ( addr len -- d f )
! 157: \ converts string addr len into d, flag indicates success
! 158: sign? >r
! 159: s>unumber?
! 160: 0= IF
! 161: rdrop false
! 162: ELSE \ no characters left, all ok
! 163: r>
! 164: IF
! 165: dnegate
! 166: THEN
! 167: true
! 168: THEN ;
! 169:
! 170: : s>number ( addr len -- d )
! 171: \ don't use this, there is no way to tell success
! 172: s>number? drop ;
! 173:
! 174: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
! 175: s>number? 0=
! 176: IF
! 177: 2drop false EXIT
! 178: THEN
! 179: dpl @ dup 0< IF
! 180: nip
! 181: ELSE
! 182: 1+
! 183: THEN ;
! 184:
! 185: : number? ( string -- string 0 / n -1 / d 0> )
! 186: dup >r count snumber? dup if
! 187: rdrop
! 188: else
! 189: r> swap
! 190: then ;
! 191:
! 192: : number ( string -- d )
! 193: number? ?dup 0= abort" ?" 0<
! 194: IF
! 195: s>d
! 196: THEN ;
! 197:
! 198: [THEN]
1.48 anton 199:
1.52 jwilke 200: hex \ the defualt base for the cross-compiler is hex !!
201: Warnings off
202:
203: \ words that are generaly useful
204:
1.59 pazsan 205: : KB 400 * ;
1.52 jwilke 206: : >wordlist ( vocabulary-xt -- wordlist-struct )
207: also execute get-order swap >r 1- set-order r> ;
208:
209: : umax 2dup u< IF swap THEN drop ;
210: : umin 2dup u> IF swap THEN drop ;
1.1 anton 211:
1.23 pazsan 212: : string, ( c-addr u -- )
213: \ puts down string as cstring
214: dup c, here swap chars dup allot move ;
1.5 pazsan 215:
1.77 ! jwilke 216: : ," [char] " parse string, ;
! 217:
1.52 jwilke 218: : SetValue ( n -- <name> )
1.54 pazsan 219: \G Same behaviour as "Value" if the <name> is not defined
220: \G Same behaviour as "to" if <name> is defined
1.52 jwilke 221: \G SetValue searches in the current vocabulary
1.68 jwilke 222: save-input bl word >r restore-input throw r> count
223: get-current search-wordlist
224: IF drop >r
225: \ we have to set current to be topmost context wordlist
226: get-order get-order get-current swap 1+ set-order
227: r> ['] to execute
1.74 jwilke 228: set-order
1.68 jwilke 229: ELSE Value THEN ;
1.52 jwilke 230:
231: : DefaultValue ( n -- <name> )
1.54 pazsan 232: \G Same behaviour as "Value" if the <name> is not defined
233: \G DefaultValue searches in the current vocabulary
1.52 jwilke 234: save-input bl word >r restore-input throw r> count
235: get-current search-wordlist
1.56 pazsan 236: IF bl word drop 2drop ELSE Value THEN ;
1.1 anton 237:
238: hex
239:
1.52 jwilke 240: \ 1 Constant Cross-Flag \ to check whether assembler compiler plug-ins are
241: \ for cross-compiling
242: \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
243:
244: : comment? ( c-addr u -- c-addr u )
245: 2dup s" (" compare 0=
246: IF postpone (
247: ELSE 2dup s" \" compare 0= IF postpone \ THEN
248: THEN ;
249:
250: \ Begin CROSS COMPILER:
251:
1.74 jwilke 252: \ debugging
1.52 jwilke 253:
1.74 jwilke 254: 0 [IF]
255:
256: This implements debugflags for the cross compiler and the compiled
257: images. It works identical to the has-flags in the environment.
258: The debugflags are defined in a vocabluary. If the word exists and
259: its value is true, the flag is switched on.
260:
261: [THEN]
262:
1.77 ! jwilke 263: >CROSS
! 264:
1.74 jwilke 265: Vocabulary debugflags \ debug flags for cross
266: also debugflags get-order over
267: Constant debugflags-wl
268: set-order previous
269:
270: : DebugFlag
271: get-current >r debugflags-wl set-current
272: SetValue
273: r> set-current ;
274:
275: : Debug? ( adr u -- flag )
276: \G return true if debug flag is defined or switched on
277: debugflags-wl search-wordlist
278: IF EXECUTE
279: ELSE false THEN ;
280:
281: : D? ( <name> -- flag )
282: \G return true if debug flag is defined or switched on
283: \G while compiling we do not return the current value but
284: bl word count debug? ;
285:
286: : [d?]
287: \G compile the value-xt so the debug flag can be switched
288: \G the flag must exist!
289: bl word count debugflags-wl search-wordlist
290: IF compile,
291: ELSE -1 ABORT" unknown debug flag"
292: \ POSTPONE false
293: THEN ; immediate
1.54 pazsan 294:
1.77 ! jwilke 295: \ \ -------------------- source file
! 296:
! 297: decimal
! 298:
! 299: Variable cross-file-list
! 300: 0 cross-file-list !
! 301: Variable target-file-list
! 302: 0 target-file-list !
! 303: Variable host-file-list
! 304: 0 host-file-list !
! 305:
! 306: cross-file-list Value file-list
! 307: 0 Value source-desc
! 308:
! 309: \ file loading
! 310:
! 311: : >fl-id 1 cells + ;
! 312: : >fl-name 2 cells + ;
! 313:
! 314: Variable filelist 0 filelist !
! 315: Create NoFile ," #load-file#"
! 316:
! 317: : loadfile ( -- adr )
! 318: source-desc ?dup IF >fl-name ELSE NoFile THEN ;
! 319:
! 320: : sourcefilename ( -- adr len )
! 321: loadfile count ;
! 322:
! 323: \ANSI : sourceline# 0 ;
! 324:
! 325: \ \ -------------------- path handling from kernel/paths.fs
! 326:
! 327: \ paths.fs path file handling 03may97jaw
! 328:
! 329: \ -Changing the search-path:
! 330: \ fpath+ <path> adds a directory to the searchpath
! 331: \ fpath= <path>|<path> makes complete now searchpath
! 332: \ seperator is |
! 333: \ .fpath displays the search path
! 334: \ remark I:
! 335: \ a ./ in the beginning of filename is expanded to the directory the
! 336: \ current file comes from. ./ can also be included in the search-path!
! 337: \ ~+/ loads from the current working directory
! 338:
! 339: \ remark II:
! 340: \ if there is no sufficient space for the search path increase it!
! 341:
! 342:
! 343: \ -Creating custom paths:
! 344:
! 345: \ It is possible to use the search mechanism on yourself.
! 346:
! 347: \ Make a buffer for the path:
! 348: \ create mypath 100 chars , \ maximum length (is checked)
! 349: \ 0 , \ real len
! 350: \ 100 chars allot \ space for path
! 351: \ use the same functions as above with:
! 352: \ mypath path+
! 353: \ mypath path=
! 354: \ mypath .path
! 355:
! 356: \ do a open with the search path:
! 357: \ open-path-file ( adr len path -- fd adr len ior )
! 358: \ the file is opened read-only; if the file is not found an error is generated
! 359:
! 360: \ questions to: wilke@jwdt.com
! 361:
! 362: [IFUNDEF] +place
! 363: : +place ( adr len adr )
! 364: 2dup >r >r
! 365: dup c@ char+ + swap move
! 366: r> r> dup c@ rot + swap c! ;
! 367: [THEN]
! 368:
! 369: [IFUNDEF] place
! 370: : place ( c-addr1 u c-addr2 )
! 371: 2dup c! char+ swap move ;
! 372: [THEN]
! 373:
! 374: \ if we have path handling, use this and the setup of it
! 375: [IFUNDEF] open-fpath-file
! 376:
! 377: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
! 378: sourcepath value fpath
! 379:
! 380: : also-path ( adr len path^ -- )
! 381: >r
! 382: \ len check
! 383: r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
! 384: \ copy into
! 385: tuck r@ cell+ dup @ cell+ + swap cmove
! 386: \ make delimiter
! 387: 0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
! 388: ;
! 389:
! 390: : only-path ( adr len path^ -- )
! 391: dup 0 swap cell+ ! also-path ;
! 392:
! 393: : path+ ( path-addr "dir" -- ) \ gforth
! 394: \G Add the directory @var{dir} to the search path @var{path-addr}.
! 395: name rot also-path ;
! 396:
! 397: : fpath+ ( "dir" ) \ gforth
! 398: \G Add directory @var{dir} to the Forth search path.
! 399: fpath path+ ;
! 400:
! 401: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
! 402: \G Make a complete new search path; the path separator is |.
! 403: name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
! 404: rot only-path ;
! 405:
! 406: : fpath= ( "dir1|dir2|dir3" ) \ gforth
! 407: \G Make a complete new Forth search path; the path separator is |.
! 408: fpath path= ;
! 409:
! 410: : path>counted cell+ dup cell+ swap @ ;
! 411:
! 412: : next-path ( adr len -- adr2 len2 )
! 413: 2dup 0 scan
! 414: dup 0= IF 2drop 0 -rot 0 -rot EXIT THEN
! 415: >r 1+ -rot r@ 1- -rot
! 416: r> - ;
! 417:
! 418: : previous-path ( path^ -- )
! 419: dup path>counted
! 420: BEGIN tuck dup WHILE repeat ;
! 421:
! 422: : .path ( path-addr -- ) \ gforth
! 423: \G Display the contents of the search path @var{path-addr}.
! 424: path>counted
! 425: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
! 426:
! 427: : .fpath ( -- ) \ gforth
! 428: \G Display the contents of the Forth search path.
! 429: fpath .path ;
! 430:
! 431: : absolut-path? ( addr u -- flag ) \ gforth
! 432: \G A path is absolute if it starts with a / or a ~ (~ expansion),
! 433: \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
! 434: \G it has a colon as second character ("C:..."). Paths simply
! 435: \G containing a / are not absolute!
! 436: 2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
! 437: over c@ [char] / = >r
! 438: over c@ [char] ~ = >r
! 439: \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
! 440: 2 min S" ./" compare 0=
! 441: r> r> r> or or or ;
! 442:
! 443: Create ofile 0 c, 255 chars allot
! 444: Create tfile 0 c, 255 chars allot
! 445:
! 446: : pathsep? dup [char] / = swap [char] \ = or ;
! 447:
! 448: : need/ ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
! 449:
! 450: : extractpath ( adr len -- adr len2 )
! 451: BEGIN dup WHILE 1-
! 452: 2dup + c@ pathsep? IF EXIT THEN
! 453: REPEAT ;
! 454:
! 455: : remove~+ ( -- )
! 456: ofile count 3 min s" ~+/" compare 0=
! 457: IF
! 458: ofile count 3 /string ofile place
! 459: THEN ;
! 460:
! 461: : expandtopic ( -- ) \ stack effect correct? - anton
! 462: \ expands "./" into an absolute name
! 463: ofile count 2 min s" ./" compare 0=
! 464: IF
! 465: ofile count 1 /string tfile place
! 466: 0 ofile c! sourcefilename extractpath ofile place
! 467: ofile c@ IF need/ THEN
! 468: tfile count over c@ pathsep? IF 1 /string THEN
! 469: ofile +place
! 470: THEN ;
! 471:
! 472: : compact.. ( adr len -- adr2 len2 )
! 473: \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
! 474: over >r -1 >r
! 475: BEGIN dup WHILE
! 476: over c@ pathsep?
! 477: IF r@ -1 =
! 478: IF r> drop dup >r
! 479: ELSE 2dup 1 /string
! 480: 3 min s" ../" compare
! 481: 0=
! 482: IF r@ over - ( diff )
! 483: 2 pick swap - ( dest-adr )
! 484: >r 3 /string r> swap 2dup >r >r
! 485: move r> r>
! 486: ELSE r> drop dup >r
! 487: THEN
! 488: THEN
! 489: THEN
! 490: 1 /string
! 491: REPEAT
! 492: r> drop
! 493: drop r> tuck - ;
! 494:
! 495: : reworkdir ( -- )
! 496: remove~+
! 497: ofile count compact..
! 498: nip ofile c! ;
! 499:
! 500: : open-ofile ( -- fid ior )
! 501: \G opens the file whose name is in ofile
! 502: expandtopic reworkdir
! 503: ofile count r/o open-file ;
! 504:
! 505: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
! 506: 0 ofile ! >r >r ofile place need/
! 507: r> r> ofile +place
! 508: open-ofile ;
! 509:
! 510: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
! 511: \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
! 512: \G If found, the resulting path and an open file descriptor
! 513: \G are returned. If the file is not found, @var{ior} is non-zero.
! 514: >r
! 515: 2dup absolut-path?
! 516: IF rdrop
! 517: ofile place open-ofile
! 518: dup 0= IF >r ofile count r> THEN EXIT
! 519: ELSE r> path>counted
! 520: BEGIN next-path dup
! 521: WHILE 5 pick 5 pick check-path
! 522: 0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
! 523: REPEAT
! 524: 2drop 2drop 2drop -38
! 525: THEN ;
! 526:
! 527: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
! 528: \G Look in the Forth search path for the file specified by @var{addr1 u1}.
! 529: \G If found, the resulting path and an open file descriptor
! 530: \G are returned. If the file is not found, @var{ior} is non-zero.
! 531: fpath open-path-file ;
! 532:
! 533: fpath= ~+
! 534:
! 535: [THEN]
! 536:
! 537: \ \ -------------------- include require 13may99jaw
! 538:
! 539: >CROSS
! 540:
! 541: : add-included-file ( adr len -- adr )
! 542: dup >fl-name char+ allocate throw >r
! 543: file-list @ r@ ! r@ file-list !
! 544: r@ >fl-name place r> ;
! 545:
! 546: : included? ( c-addr u -- f )
! 547: file-list
! 548: BEGIN @ dup
! 549: WHILE >r 2dup r@ >fl-name count compare 0=
! 550: IF rdrop 2drop true EXIT THEN
! 551: r>
! 552: REPEAT
! 553: 2drop drop false ;
! 554:
! 555: false DebugFlag showincludedfiles
! 556:
! 557: : included1 ( fd adr u -- )
! 558: \ include file adr u / fd
! 559: \ we don't use fd with include-file, because the forth system
! 560: \ doesn't know the name of the file to get a nice error report
! 561: [d?] showincludedfiles
! 562: IF cr ." Including: " 2dup type ." ..." THEN
! 563: rot close-file throw
! 564: source-desc >r
! 565: add-included-file to source-desc
! 566: sourcefilename
! 567: ['] included catch
! 568: r> to source-desc
! 569: throw ;
! 570:
! 571: : included ( adr len -- )
! 572: cross-file-list to file-list
! 573: open-fpath-file throw
! 574: included1 ;
! 575:
! 576: : required ( adr len -- )
! 577: cross-file-list to file-list
! 578: open-fpath-file throw \ 2dup cr ." R:" type
! 579: 2dup included?
! 580: IF 2drop close-file throw
! 581: ELSE included1
! 582: THEN ;
! 583:
! 584: : include bl word count included ;
! 585:
! 586: : require bl word count required ;
! 587:
! 588: also forth definitions previous
! 589:
! 590: : included ( adr len -- ) included ;
! 591:
! 592: : required ( adr len -- ) required ;
! 593:
! 594: : include include ;
! 595:
! 596: : require require ;
! 597:
! 598: >CROSS
! 599: hex
! 600:
! 601: \ \ -------------------- Error Handling 05aug97jaw
! 602:
! 603: \ Flags
! 604:
! 605: also forth definitions \ these values may be predefined before
! 606: \ the cross-compiler is loaded
! 607:
! 608: false DefaultValue stack-warn \ check on empty stack at any definition
! 609: false DefaultValue create-forward-warn \ warn on forward declaration of created words
! 610:
! 611: previous >CROSS
! 612:
! 613: : .dec
! 614: base @ decimal swap . base ! ;
! 615:
! 616: : .sourcepos
! 617: cr sourcefilename type ." :"
! 618: sourceline# .dec ;
! 619:
! 620: : warnhead
! 621: \G display error-message head
! 622: \G perhaps with linenumber and filename
! 623: .sourcepos ." Warning: " ;
! 624:
! 625: : empty? depth IF .sourcepos ." Stack not empty!" THEN ;
! 626:
! 627: stack-warn [IF]
! 628: : defempty? empty? ;
! 629: [ELSE]
! 630: : defempty? ; immediate
! 631: [THEN]
! 632:
1.52 jwilke 633: \ \ GhostNames Ghosts 9may93jaw
634:
635: \ second name source to search trough list
636:
637: VARIABLE GhostNames
638: 0 GhostNames !
639:
640: : GhostName ( -- addr )
641: here GhostNames @ , GhostNames ! here 0 ,
642: bl word count
643: \ 2dup type space
644: string, \ !! cfalign ?
645: align ;
646:
647: \ Ghost Builder 06oct92py
648:
649: \ <T T> new version with temp variable 10may93jaw
650:
651: VARIABLE VocTemp
652:
653: : <T get-current VocTemp ! also Ghosts definitions ;
654: : T> previous VocTemp @ set-current ;
655:
656: hex
657: 4711 Constant <fwd> 4712 Constant <res>
658: 4713 Constant <imm> 4714 Constant <do:>
1.75 jwilke 659: 4715 Constant <skip>
1.52 jwilke 660:
661: \ iForth makes only immediate directly after create
662: \ make atonce trick! ?
663:
664: Variable atonce atonce off
665:
1.76 jwilke 666: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
1.52 jwilke 667:
668: : GhostHeader <fwd> , 0 , ['] NoExec , ;
669:
670: : >magic ; \ type of ghost
671: : >link cell+ ; \ pointer where ghost is in target, or if unresolved
672: \ points to the where we have to resolve (linked-list)
673: : >exec cell+ cell+ ; \ execution symantics (while target compiling) of ghost
674: : >end 3 cells + ; \ room for additional tags
675: \ for builder (create, variable...) words the
676: \ execution symantics of words built are placed here
677:
678: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
679: Variable last-ghost \ last ghost that is created
680: Variable last-header-ghost \ last ghost definitions with header
681:
682: : Make-Ghost ( "name" -- ghost )
683: >in @ GhostName swap >in !
684: <T Create atonce @ IF immediate atonce off THEN
685: here tuck swap ! ghostheader T>
686: dup last-ghost !
687: DOES> dup executed-ghost ! >exec @ execute ;
688:
689: \ ghost words 14oct92py
690: \ changed: 10may93py/jaw
691:
692: : gfind ( string -- ghost true/1 / string false )
693: \ searches for string in word-list ghosts
1.77 ! jwilke 694: dup count [ ' ghosts >wordlist ] Literal search-wordlist
1.52 jwilke 695: dup IF >r >body nip r> THEN ;
696:
697: : gdiscover ( xt -- ghost true | xt false )
698: GhostNames
699: BEGIN @ dup
700: WHILE 2dup
701: cell+ @ dup >magic @ <fwd> <>
702: >r >link @ = r> and
703: IF cell+ @ nip true EXIT THEN
704: REPEAT
705: drop false ;
706:
707: VARIABLE Already
708:
709: : ghost ( "name" -- ghost )
710: Already off
1.76 jwilke 711: >in @ bl word gfind IF atonce off Already on nip EXIT THEN
1.52 jwilke 712: drop >in ! Make-Ghost ;
713:
714: : >ghostname ( ghost -- adr len )
715: GhostNames
716: BEGIN @ dup
717: WHILE 2dup cell+ @ =
718: UNTIL nip 2 cells + count
1.54 pazsan 719: ELSE 2drop
720: \ true abort" CROSS: Ghostnames inconsistent"
721: s" ?!?!?!"
1.52 jwilke 722: THEN ;
723:
1.77 ! jwilke 724: \ ' >ghostname ALIAS @name
1.52 jwilke 725:
726: : forward? ( ghost -- flag )
727: >magic @ <fwd> = ;
728:
1.75 jwilke 729: : undefined? ( ghost -- flag )
730: >magic @ dup <fwd> = swap <skip> = or ;
731:
1.52 jwilke 732: \ Predefined ghosts 12dec92py
733:
734: ghost 0= drop
735: ghost branch ghost ?branch 2drop
736: ghost (do) ghost (?do) 2drop
737: ghost (for) drop
738: ghost (loop) ghost (+loop) 2drop
739: ghost (next) drop
740: ghost unloop ghost ;S 2drop
741: ghost lit ghost (compile) ghost ! 2drop drop
742: ghost (does>) ghost noop 2drop
743: ghost (.") ghost (S") ghost (ABORT") 2drop drop
744: ghost ' drop
745: ghost :docol ghost :doesjump ghost :dodoes 2drop drop
1.54 pazsan 746: ghost :dovar drop
1.52 jwilke 747: ghost over ghost = ghost drop 2drop drop
748: ghost - drop
1.54 pazsan 749: ghost 2drop drop
750: ghost 2dup drop
1.52 jwilke 751:
752: \ \ Parameter for target systems 06oct92py
753:
754: \ we define it ans like...
755: wordlist Constant target-environment
756:
757: VARIABLE env-current \ save information of current dictionary to restore with environ>
758:
759: : >ENVIRON get-current env-current ! target-environment set-current ;
760: : ENVIRON> env-current @ set-current ;
1.43 pazsan 761:
1.48 anton 762: >TARGET
1.43 pazsan 763:
1.67 jwilke 764: : environment? ( adr len -- [ x ] true | false )
1.52 jwilke 765: target-environment search-wordlist
766: IF execute true ELSE false THEN ;
767:
1.67 jwilke 768: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
1.52 jwilke 769:
1.67 jwilke 770: : has? bl word count T environment? H
1.53 jwilke 771: IF \ environment variable is present, return its value
772: ELSE \ environment variable is not present, return false
1.75 jwilke 773: false \ debug true ABORT" arg"
1.53 jwilke 774: THEN ;
1.52 jwilke 775:
776: : $has? T environment? H IF ELSE false THEN ;
1.48 anton 777:
1.54 pazsan 778: >ENVIRON get-order get-current swap 1+ set-order
779: true SetValue compiler
1.53 jwilke 780: true SetValue cross
1.54 pazsan 781: true SetValue standard-threading
782: >TARGET previous
1.52 jwilke 783:
1.77 ! jwilke 784: 0
! 785: [IFDEF] mach-file mach-file count 1 [THEN]
! 786: [IFDEF] machine-file machine-file 1 [THEN]
! 787: [IF] included hex drop
! 788: [ELSE] cr ." No machine description!" ABORT
! 789: [THEN]
1.43 pazsan 790:
1.53 jwilke 791: >ENVIRON
792:
1.54 pazsan 793: T has? ec H
794: [IF]
795: false DefaultValue relocate
796: false DefaultValue file
797: false DefaultValue OS
798: false DefaultValue prims
799: false DefaultValue floating
800: false DefaultValue glocals
801: false DefaultValue dcomps
802: false DefaultValue hash
803: false DefaultValue xconds
804: false DefaultValue header
805: [THEN]
806:
807: true DefaultValue interpreter
808: true DefaultValue ITC
809: false DefaultValue rom
1.73 jwilke 810: true DefaultValue standardthreading
1.53 jwilke 811:
1.52 jwilke 812: >TARGET
1.53 jwilke 813: s" relocate" T environment? H
814: [IF] SetValue NIL
815: [ELSE] >ENVIRON T NIL H SetValue relocate
816: [THEN]
1.43 pazsan 817:
818: >CROSS
819:
1.52 jwilke 820: \ \ Create additional parameters 19jan95py
1.19 pazsan 821:
1.71 jwilke 822: \ currently cross only works for host machines with address-unit-bits
823: \ eual to 8 because of s! and sc!
824: \ but I start to query the environment just to modularize a little bit
825:
826: : check-address-unit-bits ( -- )
827: \ s" ADDRESS-UNIT-BITS" environment?
828: \ IF 8 <> ELSE true THEN
829: \ ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
830:
831: \ shit, this doesn't work because environment? is only defined for
832: \ gforth.fi and not kernl???.fi
833: ;
834:
835: check-address-unit-bits
836: 8 Constant bits/byte \ we define: byte is address-unit
837:
838: 1 bits/byte lshift Constant maxbyte
1.77 ! jwilke 839: \ this sets byte size for the target machine, (probably right guess) jaw
1.67 jwilke 840:
1.19 pazsan 841: T
1.71 jwilke 842: NIL Constant TNIL
843: cell Constant tcell
844: cell<< Constant tcell<<
845: cell>bit Constant tcell>bit
846: bits/char Constant tbits/char
847: bits/char H bits/byte T /
848: Constant tchar
849: float Constant tfloat
850: 1 bits/char lshift Constant tmaxchar
851: [IFUNDEF] bits/byte
852: 8 Constant tbits/byte
853: [ELSE]
854: bits/byte Constant tbits/byte
855: [THEN]
1.19 pazsan 856: H
1.71 jwilke 857: tbits/byte bits/byte / Constant tbyte
858:
1.19 pazsan 859:
1.48 anton 860: \ Variables 06oct92py
861:
862: Variable image
863: Variable tlast TNIL tlast ! \ Last name field
864: Variable tlastcfa \ Last code field
865: Variable tdoes \ Resolve does> calls
866: Variable bit$
1.52 jwilke 867:
868: \ statistics 10jun97jaw
869:
870: Variable headers-named 0 headers-named !
871: Variable user-vars 0 user-vars !
872:
1.67 jwilke 873: : target>bitmask-size ( u1 -- u2 )
874: 1- tcell>bit rshift 1+ ;
875:
876: : allocatetarget ( size --- adr )
877: dup allocate ABORT" CROSS: No memory for target"
878: swap over swap erase ;
1.52 jwilke 879:
1.54 pazsan 880: \ \ memregion.fs
1.52 jwilke 881:
882:
883: Variable last-defined-region \ pointer to last defined region
884: Variable region-link \ linked list with all regions
885: Variable mirrored-link \ linked list for mirrored regions
886: 0 dup mirrored-link ! region-link !
887:
888:
1.67 jwilke 889: : >rname 6 cells + ;
890: : >rbm 5 cells + ;
891: : >rmem 4 cells + ;
892: : >rlink 3 cells + ;
1.52 jwilke 893: : >rdp 2 cells + ;
894: : >rlen cell+ ;
895: : >rstart ;
896:
897:
898: : region ( addr len -- ) \G create a new region
899: \ check whether predefined region exists
900: save-input bl word find >r >r restore-input throw r> r> 0=
901: IF \ make region
902: drop
903: save-input create restore-input throw
904: here last-defined-region !
905: over ( startaddr ) , ( length ) , ( dp ) ,
1.67 jwilke 906: region-link linked 0 , 0 , bl word count string,
1.52 jwilke 907: ELSE \ store new parameters in region
908: bl word drop
909: >body >r r@ last-defined-region !
1.67 jwilke 910: r@ >rlen ! dup r@ >rstart ! r> >rdp !
1.52 jwilke 911: THEN ;
912:
913: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
1.67 jwilke 914: dup >rstart @ swap >rlen @ over + ;
1.52 jwilke 915:
916: : extent ( region -- startaddr len ) \G returns the really used area
1.67 jwilke 917: dup >rstart @ swap >rdp @ over - ;
1.52 jwilke 918:
919: : area ( region -- startaddr totallen ) \G returns the total area
1.67 jwilke 920: dup >rstart swap >rlen @ ;
1.52 jwilke 921:
922: : mirrored \G mark a region as mirrored
923: mirrored-link
1.68 jwilke 924: align linked last-defined-region @ , ;
1.52 jwilke 925:
1.67 jwilke 926: : .addr ( u -- )
927: \G prints a 16 or 32 Bit nice hex value
1.52 jwilke 928: base @ >r hex
929: tcell 2 u>
1.77 ! jwilke 930: IF s>d <# # # # # [char] . hold # # # # #> type
1.52 jwilke 931: ELSE s>d <# # # # # # #> type
932: THEN r> base ! ;
933:
934: : .regions \G display region statistic
935:
936: \ we want to list the regions in the right order
937: \ so first collect all regions on stack
938: 0 region-link @
939: BEGIN dup WHILE dup @ REPEAT drop
940: BEGIN dup
1.67 jwilke 941: WHILE cr
942: 0 >rlink - >r
943: r@ >rname count tuck type
1.52 jwilke 944: 12 swap - 0 max spaces space
1.67 jwilke 945: ." Start: " r@ >rstart @ dup .addr space
946: ." End: " r@ >rlen @ + .addr space
947: ." DP: " r> >rdp @ .addr
1.52 jwilke 948: REPEAT drop
1.53 jwilke 949: s" rom" T $has? H 0= ?EXIT
1.52 jwilke 950: cr ." Mirrored:"
951: mirrored-link @
952: BEGIN dup
1.67 jwilke 953: WHILE space dup cell+ @ >rname count type @
1.52 jwilke 954: REPEAT drop cr
955: ;
956:
957: \ -------- predefined regions
958:
959: 0 0 region address-space
960: \ total memory addressed and used by the target system
961:
962: 0 0 region dictionary
963: \ rom area for the compiler
964:
1.53 jwilke 965: T has? rom H
1.52 jwilke 966: [IF]
967: 0 0 region ram-dictionary mirrored
968: \ ram area for the compiler
969: [ELSE]
970: ' dictionary ALIAS ram-dictionary
971: [THEN]
972:
973: 0 0 region return-stack
974:
975: 0 0 region data-stack
976:
977: 0 0 region tib-region
978:
979: ' dictionary ALIAS rom-dictionary
980:
981:
982: : setup-target ( -- ) \G initialize targets memory space
1.53 jwilke 983: s" rom" T $has? H
1.52 jwilke 984: IF \ check for ram and rom...
1.72 jwilke 985: \ address-space area nip 0<>
1.67 jwilke 986: ram-dictionary area nip 0<>
987: rom-dictionary area nip 0<>
1.72 jwilke 988: and 0=
1.52 jwilke 989: ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
990: THEN
991: address-space area nip
992: IF
993: address-space area
994: ELSE
995: dictionary area
996: THEN
1.67 jwilke 997: nip 0=
1.52 jwilke 998: ABORT" CROSS: define at least address-space or dictionary!!"
1.67 jwilke 999:
1000: \ allocate target for each region
1001: region-link
1002: BEGIN @ dup
1003: WHILE dup
1004: 0 >rlink - >r
1005: r@ >rlen @
1006: IF \ allocate mem
1007: r@ >rlen @ dup
1008:
1009: allocatetarget dup image !
1010: r@ >rmem !
1011:
1012: target>bitmask-size allocatetarget
1.72 jwilke 1013: dup bit$ !
1.67 jwilke 1014: r> >rbm !
1015:
1016: ELSE r> drop THEN
1.72 jwilke 1017: REPEAT drop ;
1018:
1019: \ MakeKernal 22feb99jaw
1020:
1021: : makekernel ( targetsize -- targetsize )
1022: dup dictionary >rlen ! setup-target ;
1023:
1024: >MINIMAL
1025: : makekernel makekernel ;
1026: >CROSS
1.52 jwilke 1027:
1.54 pazsan 1028: \ \ switched tdp for rom support 03jun97jaw
1.52 jwilke 1029:
1030: \ second value is here to store some maximal value for statistics
1031: \ tempdp is also embedded here but has nothing to do with rom support
1032: \ (needs switched dp)
1033:
1034: variable tempdp 0 , \ temporary dp for resolving
1035: variable tempdp-save
1036:
1037: 0 [IF]
1038: variable romdp 0 , \ Dictionary-Pointer for ramarea
1039: variable ramdp 0 , \ Dictionary-Pointer for romarea
1040:
1041: \
1042: variable sramdp \ start of ram-area for forth
1043: variable sromdp \ start of rom-area for forth
1044:
1045: [THEN]
1046:
1047:
1048: 0 value tdp
1049: variable fixed \ flag: true: no automatic switching
1050: \ false: switching is done automatically
1051:
1052: \ Switch-Policy:
1053: \
1054: \ a header is always compiled into rom
1055: \ after a created word (create and variable) compilation goes to ram
1056: \
1057: \ Be careful: If you want to make the data behind create into rom
1058: \ you have to put >rom before create!
1059:
1060: variable constflag constflag off
1061:
1.67 jwilke 1062: : activate ( region -- )
1063: \G next code goes to this region
1064: >rdp to tdp ;
1065:
1.52 jwilke 1066: : (switchram)
1.53 jwilke 1067: fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
1.67 jwilke 1068: ram-dictionary activate ;
1.52 jwilke 1069:
1070: : switchram
1071: constflag @
1072: IF constflag off ELSE (switchram) THEN ;
1073:
1074: : switchrom
1.67 jwilke 1075: fixed @ ?EXIT rom-dictionary activate ;
1.52 jwilke 1076:
1077: : >tempdp ( addr -- )
1078: tdp tempdp-save ! tempdp to tdp tdp ! ;
1079: : tempdp> ( -- )
1080: tempdp-save @ to tdp ;
1081:
1082: : >ram fixed off (switchram) fixed on ;
1083: : >rom fixed off switchrom fixed on ;
1084: : >auto fixed off switchrom ;
1085:
1086:
1087:
1088: \ : romstart dup sromdp ! romdp ! ;
1089: \ : ramstart dup sramdp ! ramdp ! ;
1090:
1.67 jwilke 1091: \ default compilation goes to rom
1.52 jwilke 1092: \ when romable support is off, only the rom switch is used (!!)
1093: >auto
1094:
1.48 anton 1095: : there tdp @ ;
1096:
1.52 jwilke 1097: >TARGET
1.48 anton 1098:
1.52 jwilke 1099: \ \ Target Memory Handling
1.1 anton 1100:
1101: \ Byte ordering and cell size 06oct92py
1102:
1.19 pazsan 1103: : cell+ tcell + ;
1104: : cells tcell<< lshift ;
1.71 jwilke 1105: : chars tchar * ;
1106: : char+ tchar + ;
1.19 pazsan 1107: : floats tfloat * ;
1.6 anton 1108:
1.1 anton 1109: >CROSS
1.19 pazsan 1110: : cell/ tcell<< rshift ;
1.1 anton 1111: >TARGET
1112: 20 CONSTANT bl
1.52 jwilke 1113: \ TNIL Constant NIL
1.1 anton 1114:
1115: >CROSS
1116:
1.20 pazsan 1117: bigendian
1118: [IF]
1.52 jwilke 1119: : S! ( n addr -- ) >r s>d r> tcell bounds swap 1-
1.20 pazsan 1120: DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1.52 jwilke 1121: : S@ ( addr -- n ) >r 0 0 r> tcell bounds
1.20 pazsan 1122: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
1.57 pazsan 1123: : Sc! ( n addr -- ) >r s>d r> tchar bounds swap 1-
1124: DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1125: : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds
1126: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
1.19 pazsan 1127: [ELSE]
1.52 jwilke 1128: : S! ( n addr -- ) >r s>d r> tcell bounds
1.20 pazsan 1129: DO maxbyte ud/mod rot I c! LOOP 2drop ;
1.52 jwilke 1130: : S@ ( addr -- n ) >r 0 0 r> tcell bounds swap 1-
1.20 pazsan 1131: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
1.57 pazsan 1132: : Sc! ( n addr -- ) >r s>d r> tchar bounds
1133: DO maxbyte ud/mod rot I c! LOOP 2drop ;
1134: : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds swap 1-
1135: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
1.1 anton 1136: [THEN]
1137:
1.67 jwilke 1138: : taddr>region ( taddr -- region | 0 )
1139: \G finds for a target-address the correct region
1140: \G returns 0 if taddr is not in range of a target memory region
1141: region-link
1142: BEGIN @ dup
1143: WHILE dup >r
1144: 0 >rlink - >r
1145: r@ >rlen @
1146: IF dup r@ borders within
1147: IF r> r> drop nip EXIT THEN
1148: THEN
1149: r> drop
1150: r>
1151: REPEAT
1152: 2drop 0 ;
1153:
1154: : (>regionimage) ( taddr -- 'taddr )
1155: dup
1156: \ find region we want to address
1157: taddr>region dup 0= ABORT" Address out of range!"
1158: >r
1159: \ calculate offset in region
1160: r@ >rstart @ -
1161: \ add regions real address in our memory
1162: r> >rmem @ + ;
1163:
1.1 anton 1164: \ Bit string manipulation 06oct92py
1165: \ 9may93jaw
1166: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
1167: : bits ( n -- n ) chars Bittable + c@ ;
1168:
1169: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
1170: : +bit ( addr n -- ) >bit over c@ or swap c! ;
1.4 pazsan 1171: : -bit ( addr n -- ) >bit invert over c@ and swap c! ;
1.67 jwilke 1172:
1173: : (relon) ( taddr -- ) bit$ @ swap cell/ +bit ;
1174: : (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;
1175:
1176: : (>image) ( taddr -- absaddr ) image @ + ;
1177:
1178: DEFER >image
1179: DEFER relon
1180: DEFER reloff
1181: DEFER correcter
1182:
1183: T has? relocate H
1184: [IF]
1185: ' (relon) IS relon
1186: ' (reloff) IS reloff
1187: ' (>image) IS >image
1188: [ELSE]
1189: ' drop IS relon
1190: ' drop IS reloff
1.68 jwilke 1191: ' (>regionimage) IS >image
1.67 jwilke 1192: [THEN]
1.1 anton 1193:
1194: \ Target memory access 06oct92py
1195:
1196: : align+ ( taddr -- rest )
1.48 anton 1197: tcell tuck 1- and - [ tcell 1- ] Literal and ;
1.22 anton 1198: : cfalign+ ( taddr -- rest )
1.39 pazsan 1199: \ see kernel.fs:cfaligned
1.43 pazsan 1200: /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
1.1 anton 1201:
1202: >TARGET
1203: : aligned ( taddr -- ta-addr ) dup align+ + ;
1204: \ assumes cell alignment granularity (as GNU C)
1205:
1.22 anton 1206: : cfaligned ( taddr1 -- taddr2 )
1.39 pazsan 1207: \ see kernel.fs
1.22 anton 1208: dup cfalign+ + ;
1209:
1.52 jwilke 1210: : @ ( taddr -- w ) >image S@ ;
1211: : ! ( w taddr -- ) >image S! ;
1.57 pazsan 1212: : c@ ( taddr -- char ) >image Sc@ ;
1213: : c! ( char taddr -- ) >image Sc! ;
1.7 anton 1214: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
1215: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
1.1 anton 1216:
1217: \ Target compilation primitives 06oct92py
1218: \ included A! 16may93jaw
1219:
1220: : here ( -- there ) there ;
1221: : allot ( n -- ) tdp +! ;
1.52 jwilke 1222: : , ( w -- ) T here H tcell T allot ! H T here drop H ;
1.57 pazsan 1223: : c, ( char -- ) T here tchar allot c! H ;
1.67 jwilke 1224: : align ( -- ) T here H align+ 0 ?DO bl T c, tchar H +LOOP ;
1.22 anton 1225: : cfalign ( -- )
1.59 pazsan 1226: T here H cfalign+ 0 ?DO bl T c, tchar H +LOOP ;
1.1 anton 1227:
1.71 jwilke 1228: : >address dup 0>= IF tbyte / THEN ; \ ?? jaw
1.59 pazsan 1229: : A! swap >address swap dup relon T ! H ;
1230: : A, ( w -- ) >address T here H relon T , H ;
1.1 anton 1231:
1232: >CROSS
1233:
1.52 jwilke 1234: : tcmove ( source dest len -- )
1235: \G cmove in target memory
1.57 pazsan 1236: tchar * bounds
1.52 jwilke 1237: ?DO dup T c@ H I T c! H 1+
1.57 pazsan 1238: tchar +LOOP drop ;
1.1 anton 1239:
1.67 jwilke 1240: \ \ Load Assembler
1241:
1.1 anton 1242: >TARGET
1.74 jwilke 1243: H also Forth definitions
1.1 anton 1244:
1.77 ! jwilke 1245: : X bl word count [ ' target >wordlist ] Literal search-wordlist
1.52 jwilke 1246: IF state @ IF compile,
1247: ELSE execute THEN
1.77 ! jwilke 1248: ELSE -1 ABORT" Cross: access method not supported!"
! 1249: THEN ; immediate
1.1 anton 1250:
1.52 jwilke 1251: [IFDEF] asm-include asm-include [THEN] hex
1.1 anton 1252:
1.52 jwilke 1253: previous
1254: >CROSS H
1.1 anton 1255:
1.52 jwilke 1256: \ \ -------------------- Compiler Plug Ins 01aug97jaw
1.1 anton 1257:
1.54 pazsan 1258: \ Compiler States
1259:
1260: Variable comp-state
1261: 0 Constant interpreting
1262: 1 Constant compiling
1263: 2 Constant resolving
1264: 3 Constant assembling
1265:
1.52 jwilke 1266: Defer lit, ( n -- )
1267: Defer alit, ( n -- )
1.54 pazsan 1268:
1269: Defer branch, ( target-addr -- ) \ compiles a branch
1270: Defer ?branch, ( target-addr -- ) \ compiles a ?branch
1271: Defer branchmark, ( -- branch-addr ) \ reserves room for a branch
1272: Defer ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch
1273: Defer ?domark, ( -- branch-addr ) \ reserves room for a ?do branch
1274: Defer branchto, ( -- ) \ actual program position is target of a branch (do e.g. alignment)
1275: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
1276: Defer branchfrom, ( -- ) \ ?!
1277: Defer branchtomark, ( -- target-addr ) \ marks a branch destination
1278:
1.52 jwilke 1279: Defer colon, ( tcfa -- ) \ compiles call to tcfa at current position
1.54 pazsan 1280: Defer colonmark, ( -- addr ) \ marks a colon call
1.52 jwilke 1281: Defer colon-resolve ( tcfa addr -- )
1.54 pazsan 1282:
1.52 jwilke 1283: Defer addr-resolve ( target-addr addr -- )
1.54 pazsan 1284: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
1285:
1286: Defer do, ( -- do-token )
1287: Defer ?do, ( -- ?do-token )
1288: Defer for, ( -- for-token )
1289: Defer loop, ( do-token / ?do-token -- )
1290: Defer +loop, ( do-token / ?do-token -- )
1291: Defer next, ( for-token )
1.1 anton 1292:
1.52 jwilke 1293: [IFUNDEF] ca>native
1294: defer ca>native
1295: [THEN]
1.1 anton 1296:
1.52 jwilke 1297: >TARGET
1298: DEFER >body \ we need the system >body
1299: \ and the target >body
1300: >CROSS
1301: T 2 cells H VALUE xt>body
1.54 pazsan 1302: DEFER doprim, \ compiles start of a primitive
1303: DEFER docol, \ compiles start of a colon definition
1.52 jwilke 1304: DEFER doer,
1305: DEFER fini, \ compiles end of definition ;s
1306: DEFER doeshandler,
1307: DEFER dodoes,
1308:
1309: DEFER ]comp \ starts compilation
1310: DEFER comp[ \ ends compilation
1311:
1.54 pazsan 1312: : (cc) T a, H ; ' (cc) IS colon,
1313:
1314: : (cr) >tempdp ]comp colon, comp[ tempdp> ; ' (cr) IS colon-resolve
1315: : (ar) T ! H ; ' (ar) IS addr-resolve
1316: : (dr) ( ghost res-pnt target-addr addr )
1317: >tempdp drop over
1318: dup >magic @ <do:> =
1319: IF doer,
1320: ELSE dodoes,
1321: THEN
1322: tempdp> ; ' (dr) IS doer-resolve
1323:
1324: : (cm) ( -- addr )
1325: T here align H
1326: -1 colon, ; ' (cm) IS colonmark,
1.1 anton 1327:
1.52 jwilke 1328: >TARGET
1329: : compile, colon, ;
1330: >CROSS
1.1 anton 1331:
1.52 jwilke 1332: \ resolve structure
1.1 anton 1333:
1.52 jwilke 1334: : >next ; \ link to next field
1.54 pazsan 1335: : >tag cell+ ; \ indecates type of reference: 0: call, 1: address, 2: doer
1.53 jwilke 1336: : >taddr cell+ cell+ ;
1.52 jwilke 1337: : >ghost 3 cells + ;
1.53 jwilke 1338: : >file 4 cells + ;
1339: : >line 5 cells + ;
1.48 anton 1340:
1.54 pazsan 1341: : (refered) ( ghost addr tag -- )
1342: \G creates a reference to ghost at address taddr
1343: rot >r here r@ >link @ , r> >link !
1344: ( taddr tag ) ,
1345: ( taddr ) ,
1346: last-header-ghost @ ,
1347: loadfile ,
1348: sourceline# ,
1349: ;
1350:
1.52 jwilke 1351: : refered ( ghost tag -- )
1.53 jwilke 1352: \G creates a resolve structure
1.54 pazsan 1353: T here aligned H swap (refered)
1354: ;
1355:
1356: : killref ( addr ghost -- )
1357: \G kills a forward reference to ghost at position addr
1358: \G this is used to eleminate a :dovar refence after making a DOES>
1359: dup >magic @ <fwd> <> IF 2drop EXIT THEN
1360: swap >r >link
1361: BEGIN dup @ dup ( addr last this )
1362: WHILE dup >taddr @ r@ =
1363: IF @ over !
1364: ELSE nip THEN
1365: REPEAT rdrop 2drop
1.53 jwilke 1366: ;
1.48 anton 1367:
1.52 jwilke 1368: Defer resolve-warning
1.1 anton 1369:
1.52 jwilke 1370: : reswarn-test ( ghost res-struct -- ghost res-struct )
1371: over cr ." Resolving " >ghostname type dup ." in " >ghost @ >ghostname type ;
1.1 anton 1372:
1.52 jwilke 1373: : reswarn-forward ( ghost res-struct -- ghost res-struct )
1374: over warnhead >ghostname type dup ." is referenced in "
1375: >ghost @ >ghostname type ;
1.1 anton 1376:
1.52 jwilke 1377: \ ' reswarn-test IS resolve-warning
1378:
1.1 anton 1379: \ resolve 14oct92py
1380:
1.54 pazsan 1381: : resolve-loop ( ghost resolve-list tcfa -- )
1382: >r
1383: BEGIN dup WHILE
1384: \ dup >tag @ 2 = IF reswarn-forward THEN
1385: resolve-warning
1386: r@ over >taddr @
1387: 2 pick >tag @
1388: CASE 0 OF colon-resolve ENDOF
1389: 1 OF addr-resolve ENDOF
1390: 2 OF doer-resolve ENDOF
1391: ENDCASE
1392: @ \ next list element
1393: REPEAT 2drop rdrop
1394: ;
1.52 jwilke 1395:
1396: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
1397: \ >r dup >link @
1398: \ BEGIN dup WHILE dup T @ H r@ rot T ! H REPEAT drop r> ;
1.1 anton 1399:
1400: \ exists 9may93jaw
1401:
1.52 jwilke 1402: Variable TWarnings
1403: TWarnings on
1404: Variable Exists-Warnings
1405: Exists-Warnings on
1406:
1.1 anton 1407: : exists ( ghost tcfa -- )
1408: over GhostNames
1409: BEGIN @ dup
1410: WHILE 2dup cell+ @ =
1411: UNTIL
1.52 jwilke 1412: 2 cells + count
1413: TWarnings @ Exists-Warnings @ and
1414: IF warnhead type ." exists"
1415: ELSE 2drop THEN
1416: drop swap >link !
1.24 pazsan 1417: ELSE true abort" CROSS: Ghostnames inconsistent "
1.1 anton 1418: THEN ;
1419:
1420: : resolve ( ghost tcfa -- )
1.54 pazsan 1421: \G resolve referencies to ghost with tcfa
1422: \ is ghost resolved?, second resolve means another definition with the
1423: \ same name
1.75 jwilke 1424: over undefined? 0= IF exists EXIT THEN
1.54 pazsan 1425: \ get linked-list
1426: swap >r r@ >link @ swap \ ( list tcfa R: ghost )
1427: \ mark ghost as resolved
1428: dup r@ >link ! <res> r@ >magic !
1429: \ loop through forward referencies
1430: r> -rot
1431: comp-state @ >r Resolving comp-state !
1432: resolve-loop
1433: r> comp-state !
1434:
1435: ['] noop IS resolve-warning
1.52 jwilke 1436: ;
1.1 anton 1437:
1438: \ gexecute ghost, 01nov92py
1439:
1.52 jwilke 1440: : is-forward ( ghost -- )
1.54 pazsan 1441: colonmark, 0 (refered) ; \ compile space for call
1.52 jwilke 1442:
1443: : is-resolved ( ghost -- )
1444: >link @ colon, ; \ compile-call
1445:
1446: : gexecute ( ghost -- )
1447: dup @ <fwd> = IF is-forward ELSE is-resolved THEN ;
1448:
1449: : addr, ( ghost -- )
1450: dup @ <fwd> = IF 1 refered 0 T a, H ELSE >link @ T a, H THEN ;
1451:
1452: \ !! : ghost, ghost gexecute ;
1.1 anton 1453:
1454: \ .unresolved 11may93jaw
1455:
1456: variable ResolveFlag
1457:
1458: \ ?touched 11may93jaw
1459:
1.52 jwilke 1460: : ?touched ( ghost -- flag ) dup forward? swap >link @
1.1 anton 1461: 0 <> and ;
1462:
1.53 jwilke 1463: : .forwarddefs ( ghost -- )
1464: ." appeared in:"
1465: >link
1466: BEGIN @ dup
1467: WHILE cr 5 spaces
1468: dup >ghost @ >ghostname type
1469: ." file " dup >file @ ?dup IF count type ELSE ." CON" THEN
1470: ." line " dup >line @ .dec
1471: REPEAT
1472: drop ;
1473:
1.1 anton 1474: : ?resolved ( ghostname -- )
1475: dup cell+ @ ?touched
1.53 jwilke 1476: IF dup
1477: cell+ cell+ count cr type ResolveFlag on
1478: cell+ @ .forwarddefs
1479: ELSE drop
1480: THEN ;
1.1 anton 1481:
1482: >MINIMAL
1483: : .unresolved ( -- )
1484: ResolveFlag off cr ." Unresolved: "
1485: Ghostnames
1486: BEGIN @ dup
1487: WHILE dup ?resolved
1.10 anton 1488: REPEAT drop ResolveFlag @
1489: IF
1.48 anton 1490: -1 abort" Unresolved words!"
1.10 anton 1491: ELSE
1492: ." Nothing!"
1493: THEN
1494: cr ;
1.1 anton 1495:
1.52 jwilke 1496: : .stats
1497: base @ >r decimal
1498: cr ." named Headers: " headers-named @ .
1499: r> base ! ;
1500:
1.1 anton 1501: >CROSS
1502: \ Header states 12dec92py
1503:
1504: : flag! ( 8b -- ) tlast @ dup >r T c@ xor r> c! H ;
1505:
1506: VARIABLE ^imm
1507:
1508: >TARGET
1.36 anton 1509: : immediate 40 flag!
1.18 pazsan 1510: ^imm @ @ dup <imm> = IF drop EXIT THEN
1.1 anton 1511: <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
1512: <imm> ^imm @ ! ;
1.36 anton 1513: : restrict 20 flag! ;
1.52 jwilke 1514:
1.54 pazsan 1515: : isdoer
1516: \G define a forth word as doer, this makes obviously only sence on
1517: \G forth processors such as the PSC1000
1518: <do:> last-header-ghost @ >magic ! ;
1.1 anton 1519: >CROSS
1520:
1521: \ ALIAS2 ansforth conform alias 9may93jaw
1522:
1523: : ALIAS2 create here 0 , DOES> @ execute ;
1524: \ usage:
1.18 pazsan 1525: \ ' <name> alias2 bla !
1.1 anton 1526:
1527: \ Target Header Creation 01nov92py
1528:
1.52 jwilke 1529: >TARGET
1.1 anton 1530: : string, ( addr count -- )
1.28 pazsan 1531: dup T c, H bounds ?DO I c@ T c, H LOOP ;
1.52 jwilke 1532: : name, ( "name" -- ) bl word count T string, cfalign H ;
1.1 anton 1533: : view, ( -- ) ( dummy ) ;
1.52 jwilke 1534: >CROSS
1.1 anton 1535:
1.25 pazsan 1536: \ Target Document Creation (goes to crossdoc.fd) 05jul95py
1537:
1.55 pazsan 1538: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
1.25 pazsan 1539: \ contains the file-id of the documentation file
1540:
1.40 pazsan 1541: : T-\G ( -- )
1.25 pazsan 1542: source >in @ /string doc-file-id write-line throw
1.40 pazsan 1543: postpone \ ;
1.25 pazsan 1544:
1.39 pazsan 1545: Variable to-doc to-doc on
1.25 pazsan 1546:
1547: : cross-doc-entry ( -- )
1548: to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header
1549: IF
1550: s" " doc-file-id write-line throw
1551: s" make-doc " doc-file-id write-file throw
1.77 ! jwilke 1552:
! 1553: tlast @ >image count 1F and doc-file-id write-file throw
1.25 pazsan 1554: >in @
1555: [char] ( parse 2drop
1556: [char] ) parse doc-file-id write-file throw
1557: s" )" doc-file-id write-file throw
1558: [char] \ parse 2drop
1.40 pazsan 1559: T-\G
1.25 pazsan 1560: >in !
1.39 pazsan 1561: THEN ;
1.25 pazsan 1562:
1.28 pazsan 1563: \ Target TAGS creation
1564:
1.39 pazsan 1565: s" kernel.TAGS" r/w create-file throw value tag-file-id
1.28 pazsan 1566: \ contains the file-id of the tags file
1567:
1568: Create tag-beg 2 c, 7F c, bl c,
1569: Create tag-end 2 c, bl c, 01 c,
1570: Create tag-bof 1 c, 0C c,
1571:
1572: 2variable last-loadfilename 0 0 last-loadfilename 2!
1573:
1574: : put-load-file-name ( -- )
1.77 ! jwilke 1575: sourcefilename last-loadfilename 2@ d<>
1.28 pazsan 1576: IF
1577: tag-bof count tag-file-id write-line throw
1.31 anton 1578: sourcefilename 2dup
1.28 pazsan 1579: tag-file-id write-file throw
1580: last-loadfilename 2!
1581: s" ,0" tag-file-id write-line throw
1582: THEN ;
1583:
1584: : cross-tag-entry ( -- )
1585: tlast @ 0<> \ not an anonymous (i.e. noname) header
1586: IF
1587: put-load-file-name
1588: source >in @ min tag-file-id write-file throw
1589: tag-beg count tag-file-id write-file throw
1.77 ! jwilke 1590: tlast @ >image count 1F and tag-file-id write-file throw
1.28 pazsan 1591: tag-end count tag-file-id write-file throw
1.31 anton 1592: base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1.28 pazsan 1593: \ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
1594: s" ,0" tag-file-id write-line throw
1595: base !
1596: THEN ;
1597:
1.43 pazsan 1598: \ Check for words
1599:
1600: Defer skip? ' false IS skip?
1601:
1.75 jwilke 1602: : skipdef ( <name> -- )
1603: \G skip definition of an undefined word in undef-words mode
1604: ghost dup forward?
1605: IF >magic <skip> swap !
1606: ELSE drop THEN ;
1607:
1.77 ! jwilke 1608: : tdefined? ( -- flag ) \ name
1.75 jwilke 1609: ghost undefined? 0= ;
1610:
1611: : defined2? ( -- flag ) \ name
1612: \G return true for anything else than forward, even for <skip>
1613: \G that's what we want
1.52 jwilke 1614: ghost forward? 0= ;
1.43 pazsan 1615:
1616: : needed? ( -- flag ) \ name
1.48 anton 1617: \G returns a false flag when
1618: \G a word is not defined
1619: \G a forward reference exists
1620: \G so the definition is not skipped!
1621: bl word gfind
1.75 jwilke 1622: IF dup undefined?
1.48 anton 1623: nip
1624: 0=
1625: ELSE drop true THEN ;
1.43 pazsan 1626:
1.44 pazsan 1627: : doer? ( -- flag ) \ name
1628: ghost >magic @ <do:> = ;
1629:
1.43 pazsan 1630: : skip-defs ( -- )
1631: BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ;
1632:
1.28 pazsan 1633: \ Target header creation
1634:
1.54 pazsan 1635: Variable CreateFlag
1636: CreateFlag off
1.52 jwilke 1637:
1.54 pazsan 1638: Variable NoHeaderFlag
1639: NoHeaderFlag off
1.1 anton 1640:
1.54 pazsan 1641: : 0.r ( n1 n2 -- )
1642: base @ >r hex
1643: 0 swap <# 0 ?DO # LOOP #> type
1644: r> base ! ;
1.52 jwilke 1645: : .sym
1646: bounds
1647: DO I c@ dup
1.77 ! jwilke 1648: CASE [char] / OF drop ." \/" ENDOF
! 1649: [char] \ OF drop ." \\" ENDOF
1.52 jwilke 1650: dup OF emit ENDOF
1651: ENDCASE
1.54 pazsan 1652: LOOP ;
1.52 jwilke 1653:
1.43 pazsan 1654: : (Theader ( "name" -- ghost )
1.54 pazsan 1655: \ >in @ bl word count type 2 spaces >in !
1656: \ wordheaders will always be compiled to rom
1657: switchrom
1658: \ build header in target
1659: NoHeaderFlag @
1660: IF NoHeaderFlag off
1661: ELSE
1662: T align H view,
1.59 pazsan 1663: tlast @ dup 0> IF T 1 cells - H THEN T A, H there tlast !
1.54 pazsan 1664: 1 headers-named +! \ Statistic
1665: >in @ T name, H >in !
1666: THEN
1667: T cfalign here H tlastcfa !
1668: \ Symbol table
1669: \ >in @ cr ." sym:s/CFA=" there 4 0.r ." /" bl word count .sym ." /g" cr >in !
1670: CreateFlag @
1.76 jwilke 1671: IF \ for a created word we need also a definition in target
1672: \ to execute the created word while compile time
1673: \ dont mind if a alias is defined twice
1674: Warnings @ >r Warnings off
1.54 pazsan 1675: >in @ alias2 swap >in ! \ create alias in target
1.76 jwilke 1676: r> Warnings !
1.54 pazsan 1677: >in @ ghost swap >in !
1678: swap also ghosts ' previous swap ! \ tick ghost and store in alias
1679: CreateFlag off
1680: ELSE ghost
1681: THEN
1682: dup Last-Header-Ghost !
1683: dup >magic ^imm ! \ a pointer for immediate
1684: Already @
1685: IF dup >end tdoes !
1686: ELSE 0 tdoes !
1687: THEN
1688: 80 flag!
1689: cross-doc-entry cross-tag-entry ;
1.1 anton 1690:
1691: VARIABLE ;Resolve 1 cells allot
1.52 jwilke 1692: \ this is the resolver information from ":"
1693: \ resolving is done by ";"
1.1 anton 1694:
1.11 pazsan 1695: : Theader ( "name" -- ghost )
1696: (THeader dup there resolve 0 ;Resolve ! ;
1.1 anton 1697:
1698: >TARGET
1699: : Alias ( cfa -- ) \ name
1.43 pazsan 1700: >in @ skip? IF 2drop EXIT THEN >in !
1.53 jwilke 1701: dup 0< s" prims" T $has? H 0= and
1.43 pazsan 1702: IF
1.53 jwilke 1703: .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
1.43 pazsan 1704: THEN
1705: (THeader over resolve T A, H 80 flag! ;
1.42 pazsan 1706: : Alias: ( cfa -- ) \ name
1.43 pazsan 1707: >in @ skip? IF 2drop EXIT THEN >in !
1.53 jwilke 1708: dup 0< s" prims" T $has? H 0= and
1.43 pazsan 1709: IF
1.53 jwilke 1710: .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
1.43 pazsan 1711: THEN
1712: ghost tuck swap resolve <do:> swap >magic ! ;
1.64 pazsan 1713:
1714: Variable prim#
1715: : first-primitive ( n -- ) prim# ! ;
1716: : Primitive ( -- ) \ name
1717: prim# @ T Alias H -1 prim# +! ;
1.1 anton 1718: >CROSS
1719:
1720: \ Conditionals and Comments 11may93jaw
1721:
1722: : ;Cond
1723: postpone ;
1724: swap ! ; immediate
1725:
1726: : Cond: ( -- ) \ name {code } ;
1727: atonce on
1728: ghost
1729: >exec
1730: :NONAME ;
1731:
1732: : restrict? ( -- )
1733: \ aborts on interprete state - ae
1734: state @ 0= ABORT" CROSS: Restricted" ;
1735:
1736: : Comment ( -- )
1737: >in @ atonce on ghost swap >in ! ' swap >exec ! ;
1738:
1739: Comment ( Comment \
1740:
1741: \ compile 10may93jaw
1742:
1743: : compile ( -- ) \ name
1744: restrict?
1.13 pazsan 1745: bl word gfind dup 0= ABORT" CROSS: Can't compile "
1.1 anton 1746: 0> ( immediate? )
1747: IF >exec @ compile,
1748: ELSE postpone literal postpone gexecute THEN ;
1749: immediate
1750:
1.52 jwilke 1751: : [G']
1752: \G ticks a ghost and returns its address
1753: bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1754: state @
1755: IF postpone literal
1756: THEN ; immediate
1757:
1758: : ghost>cfa
1.75 jwilke 1759: dup undefined? ABORT" CROSS: forward " >link @ ;
1.52 jwilke 1760:
1761: >TARGET
1762:
1763: : ' ( -- cfa )
1764: \ returns the target-cfa of a ghost
1765: bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1766: ghost>cfa ;
1767:
1768: Cond: ['] T ' H alit, ;Cond
1769:
1770: >CROSS
1771:
1772: : [T']
1773: \ returns the target-cfa of a ghost, or compiles it as literal
1774: postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
1.42 pazsan 1775:
1.52 jwilke 1776: \ \ threading modell 13dec92py
1777: \ modularized 14jun97jaw
1778:
1779: : fillcfa ( usedcells -- )
1.59 pazsan 1780: T cells H xt>body swap - 0 ?DO 0 T c, tchar H +LOOP ;
1.52 jwilke 1781:
1782: : (>body) ( cfa -- pfa ) xt>body + ; ' (>body) T IS >body H
1783:
1784: : (doer,) ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ; ' (doer,) IS doer,
1785:
1786: : (docol,) ( -- ) [G'] :docol doer, ; ' (docol,) IS docol,
1787:
1788: : (doprim,) ( -- )
1789: there xt>body + ca>native T a, H 1 fillcfa ; ' (doprim,) IS doprim,
1790:
1791: : (doeshandler,) ( -- )
1792: T cfalign H compile :doesjump T 0 , H ; ' (doeshandler,) IS doeshandler,
1793:
1794: : (dodoes,) ( does-action-ghost -- )
1795: ]comp [G'] :dodoes gexecute comp[
1796: addr,
1797: T here H tcell - reloff 2 fillcfa ; ' (dodoes,) IS dodoes,
1798:
1799: : (lit,) ( n -- ) compile lit T , H ; ' (lit,) IS lit,
1800:
1.67 jwilke 1801: \ if we dont produce relocatable code alit, defaults to lit, jaw
1802: has? relocate
1803: [IF]
1.66 pazsan 1804: : (alit,) ( n -- ) compile lit T a, H ; ' (alit,) IS alit,
1.67 jwilke 1805: [ELSE]
1806: : (alit,) ( n -- ) lit, ; ' (alit,) IS alit,
1807: [THEN]
1.52 jwilke 1808:
1809: : (fini,) compile ;s ; ' (fini,) IS fini,
1.42 pazsan 1810:
1.43 pazsan 1811: [IFUNDEF] (code)
1812: Defer (code)
1813: Defer (end-code)
1814: [THEN]
1815:
1.1 anton 1816: >TARGET
1.43 pazsan 1817: : Code
1.52 jwilke 1818: defempty?
1.48 anton 1819: (THeader there resolve
1.53 jwilke 1820: [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
1.52 jwilke 1821: doprim,
1.48 anton 1822: [THEN]
1823: depth (code) ;
1.43 pazsan 1824:
1825: : Code:
1.52 jwilke 1826: defempty?
1.48 anton 1827: ghost dup there ca>native resolve <do:> swap >magic !
1.43 pazsan 1828: depth (code) ;
1829:
1830: : end-code
1.52 jwilke 1831: (end-code)
1.43 pazsan 1832: depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1833: ELSE true ABORT" CROSS: Stack empty" THEN
1.52 jwilke 1834: ;
1.14 anton 1835:
1.57 pazsan 1836: ( Cond ) : chars tchar * ; ( Cond )
1.1 anton 1837:
1838: >CROSS
1.52 jwilke 1839:
1.1 anton 1840: \ tLiteral 12dec92py
1841:
1842: >TARGET
1.40 pazsan 1843: Cond: \G T-\G ;Cond
1844:
1.1 anton 1845: Cond: Literal ( n -- ) restrict? lit, ;Cond
1846: Cond: ALiteral ( n -- ) restrict? alit, ;Cond
1847:
1848: : Char ( "<char>" -- ) bl word char+ c@ ;
1849: Cond: [Char] ( "<char>" -- ) restrict? Char lit, ;Cond
1850:
1.43 pazsan 1851: \ some special literals 27jan97jaw
1852:
1.52 jwilke 1853: \ !! Known Bug: Special Literals and plug-ins work only correct
1854: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
1855:
1.43 pazsan 1856: Cond: MAXU
1.52 jwilke 1857: restrict?
1858: tcell 1 cells u>
1859: IF compile lit tcell 0 ?DO FF T c, H LOOP
1.77 ! jwilke 1860: ELSE ffffffff lit, THEN
1.52 jwilke 1861: ;Cond
1.43 pazsan 1862:
1863: Cond: MINI
1.52 jwilke 1864: restrict?
1865: tcell 1 cells u>
1866: IF compile lit bigendian
1867: IF 80 T c, H tcell 1 ?DO 0 T c, H LOOP
1868: ELSE tcell 1 ?DO 0 T c, H LOOP 80 T c, H
1869: THEN
1.77 ! jwilke 1870: ELSE tcell 2 = IF 8000 ELSE 80000000 THEN lit, THEN
1.52 jwilke 1871: ;Cond
1.43 pazsan 1872:
1873: Cond: MAXI
1.52 jwilke 1874: restrict?
1875: tcell 1 cells u>
1876: IF compile lit bigendian
1877: IF 7F T c, H tcell 1 ?DO FF T c, H LOOP
1878: ELSE tcell 1 ?DO FF T c, H LOOP 7F T c, H
1879: THEN
1.77 ! jwilke 1880: ELSE tcell 2 = IF 7fff ELSE 7fffffff THEN lit, THEN
1.43 pazsan 1881: ;Cond
1882:
1.1 anton 1883: >CROSS
1884: \ Target compiling loop 12dec92py
1885: \ ">tib trick thrown out 10may93jaw
1886: \ number? defined at the top 11may93jaw
1.77 ! jwilke 1887: \ replaced >in by save-input
! 1888:
! 1889: : discard 0 ?DO drop LOOP ;
1.1 anton 1890:
1891: \ compiled word might leave items on stack!
1.77 ! jwilke 1892: : tcom ( x1 .. xn n name -- )
! 1893: \ dup count type space
! 1894: gfind ?dup
! 1895: IF >r >r discard r> r>
! 1896: 0> IF >exec @ execute
! 1897: ELSE gexecute THEN
! 1898: EXIT
! 1899: THEN
! 1900: number? dup
! 1901: IF 0> IF swap lit, THEN lit, discard
! 1902: ELSE 2drop restore-input throw ghost gexecute THEN ;
1.1 anton 1903:
1904: >TARGET
1905: \ : ; DOES> 13dec92py
1906: \ ] 9may93py/jaw
1907:
1908: : ] state on
1.54 pazsan 1909: Compiling comp-state !
1.1 anton 1910: BEGIN
1.77 ! jwilke 1911: BEGIN save-input bl word
! 1912: dup c@ 0= WHILE drop discard refill 0=
1.1 anton 1913: ABORT" CROSS: End of file while target compiling"
1914: REPEAT
1915: tcom
1916: state @
1917: 0=
1918: UNTIL ;
1919:
1920: \ by the way: defining a second interpreter (a compiler-)loop
1921: \ is not allowed if a system should be ans conform
1922:
1923: : : ( -- colon-sys ) \ Name
1.52 jwilke 1924: defempty?
1925: constflag off \ don't let this flag work over colon defs
1926: \ just to go sure nothing unwanted happens
1.43 pazsan 1927: >in @ skip? IF drop skip-defs EXIT THEN >in !
1.1 anton 1928: (THeader ;Resolve ! there ;Resolve cell+ !
1.52 jwilke 1929: docol, ]comp depth T ] H ;
1.1 anton 1930:
1.37 pazsan 1931: : :noname ( -- colon-sys )
1.52 jwilke 1932: T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
1.37 pazsan 1933:
1.2 pazsan 1934: Cond: EXIT ( -- ) restrict? compile ;S ;Cond
1.6 anton 1935:
1936: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2 pazsan 1937:
1.52 jwilke 1938: >CROSS
1939: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
1940: ;Resolve cell+ @ ;
1941:
1942: >TARGET
1943:
1944: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
1945:
1.1 anton 1946: Cond: ; ( -- ) restrict?
1947: depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1948: ELSE true ABORT" CROSS: Stack empty" THEN
1.52 jwilke 1949: fini,
1950: comp[
1951: state off
1.1 anton 1952: ;Resolve @
1953: IF ;Resolve @ ;Resolve cell+ @ resolve THEN
1.54 pazsan 1954: Interpreting comp-state !
1.1 anton 1955: ;Cond
1.54 pazsan 1956: Cond: [ restrict? state off Interpreting comp-state ! ;Cond
1.1 anton 1957:
1958: >CROSS
1.54 pazsan 1959:
1960: Create GhostDummy ghostheader
1961: <res> GhostDummy >magic !
1962:
1.52 jwilke 1963: : !does ( does-action -- )
1964: \ !! zusammenziehen und dodoes, machen!
1.54 pazsan 1965: tlastcfa @ [G'] :dovar killref
1966: \ tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
1.52 jwilke 1967: \ !! geht so nicht, da dodoes, ghost will!
1.54 pazsan 1968: GhostDummy >link ! GhostDummy
1969: tlastcfa @ >tempdp dodoes, tempdp> ;
1.1 anton 1970:
1971: >TARGET
1972: Cond: DOES> restrict?
1.52 jwilke 1973: compile (does>) doeshandler,
1974: \ resolve words made by builders
1975: tdoes @ ?dup IF @ T here H resolve THEN
1.1 anton 1976: ;Cond
1.52 jwilke 1977: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
1.1 anton 1978:
1979: >CROSS
1980: \ Creation 01nov92py
1981:
1982: \ Builder 11may93jaw
1983:
1.52 jwilke 1984: : Builder ( Create-xt do:-xt "name" -- )
1985: \ builds up a builder in current vocabulary
1986: \ create-xt is executed when word is interpreted
1987: \ do:-xt is executet when the created word from builder is executed
1988: \ for do:-xt an additional entry after the normal ghost-enrys is used
1989:
1.1 anton 1990: >in @ alias2 swap dup >in ! >r >r
1.54 pazsan 1991: Make-Ghost
1992: rot swap >exec dup @ ['] NoExec <>
1993: IF 2drop ELSE ! THEN
1994: ,
1.1 anton 1995: r> r> >in !
1.11 pazsan 1996: also ghosts ' previous swap ! ;
1997: \ DOES> dup >exec @ execute ;
1.1 anton 1998:
1.52 jwilke 1999: : gdoes, ( ghost -- )
2000: \ makes the codefield for a word that is built
1.75 jwilke 2001: >end @ dup undefined? 0=
1.52 jwilke 2002: IF
1.42 pazsan 2003: dup >magic @ <do:> =
1.54 pazsan 2004: IF doer,
2005: ELSE dodoes,
2006: THEN
2007: EXIT
1.52 jwilke 2008: THEN
2009: \ compile :dodoes gexecute
2010: \ T here H tcell - reloff
1.54 pazsan 2011: 2 refered
2012: 0 fillcfa
2013: ;
1.1 anton 2014:
1.52 jwilke 2015: : TCreate ( <name> -- )
2016: executed-ghost @
1.1 anton 2017: CreateFlag on
1.52 jwilke 2018: create-forward-warn
2019: IF ['] reswarn-forward IS resolve-warning THEN
1.11 pazsan 2020: Theader >r dup gdoes,
1.77 ! jwilke 2021: \ stores execution semantic in the built word
1.52 jwilke 2022: >end @ >exec @ r> >exec ! ;
2023:
2024: : RTCreate ( <name> -- )
2025: \ creates a new word with code-field in ram
2026: executed-ghost @
2027: CreateFlag on
2028: create-forward-warn
2029: IF ['] reswarn-forward IS resolve-warning THEN
2030: \ make Alias
2031: (THeader there 0 T a, H 80 flag! ( S executed-ghost new-ghost )
2032: \ store poiter to code-field
2033: switchram T cfalign H
2034: there swap T ! H
2035: there tlastcfa !
2036: dup there resolve 0 ;Resolve !
2037: >r dup gdoes,
1.11 pazsan 2038: >end @ >exec @ r> >exec ! ;
1.1 anton 2039:
2040: : Build: ( -- [xt] [colon-sys] )
1.52 jwilke 2041: :noname postpone TCreate ;
2042:
2043: : BuildSmart: ( -- [xt] [colon-sys] )
2044: :noname
1.53 jwilke 2045: [ T has? rom H [IF] ]
1.52 jwilke 2046: postpone RTCreate
2047: [ [ELSE] ]
2048: postpone TCreate
2049: [ [THEN] ] ;
1.1 anton 2050:
2051: : gdoes> ( ghost -- addr flag )
1.52 jwilke 2052: executed-ghost @
1.1 anton 2053: state @ IF gexecute true EXIT THEN
1.52 jwilke 2054: >link @ T >body H false ;
1.1 anton 2055:
2056: \ DO: ;DO 11may93jaw
2057: \ changed to ?EXIT 10may93jaw
2058:
2059: : DO: ( -- addr [xt] [colon-sys] )
2060: here ghostheader
1.11 pazsan 2061: :noname postpone gdoes> postpone ?EXIT ;
1.1 anton 2062:
1.42 pazsan 2063: : by: ( -- addr [xt] [colon-sys] ) \ name
2064: ghost
2065: :noname postpone gdoes> postpone ?EXIT ;
2066:
1.52 jwilke 2067: : ;DO ( addr [xt] [colon-sys] -- addr )
1.1 anton 2068: postpone ; ( S addr xt )
2069: over >exec ! ; immediate
2070:
2071: : by ( -- addr ) \ Name
2072: ghost >end @ ;
2073:
2074: >TARGET
2075: \ Variables and Constants 05dec92py
2076:
1.52 jwilke 2077: Build: ( n -- ) ;
2078: by: :docon ( ghost -- n ) T @ H ;DO
2079: Builder (Constant)
2080:
2081: Build: ( n -- ) T , H ;
2082: by (Constant)
2083: Builder Constant
2084:
2085: Build: ( n -- ) T A, H ;
2086: by (Constant)
2087: Builder AConstant
2088:
2089: Build: ( d -- ) T , , H ;
2090: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
2091: Builder 2Constant
2092:
2093: BuildSmart: ;
1.42 pazsan 2094: by: :dovar ( ghost -- addr ) ;DO
1.1 anton 2095: Builder Create
2096:
1.53 jwilke 2097: T has? rom H [IF]
1.54 pazsan 2098: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
1.52 jwilke 2099: by (Constant)
2100: Builder Variable
2101: [ELSE]
1.1 anton 2102: Build: T 0 , H ;
2103: by Create
2104: Builder Variable
1.52 jwilke 2105: [THEN]
1.1 anton 2106:
1.53 jwilke 2107: T has? rom H [IF]
1.54 pazsan 2108: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
2109: by (Constant)
2110: Builder 2Variable
2111: [ELSE]
2112: Build: T 0 , 0 , H ;
2113: by Create
2114: Builder 2Variable
2115: [THEN]
2116:
2117: T has? rom H [IF]
2118: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
1.52 jwilke 2119: by (Constant)
2120: Builder AVariable
2121: [ELSE]
1.1 anton 2122: Build: T 0 A, H ;
2123: by Create
2124: Builder AVariable
1.52 jwilke 2125: [THEN]
1.1 anton 2126:
1.3 pazsan 2127: \ User variables 04may94py
2128:
2129: >CROSS
2130: Variable tup 0 tup !
2131: Variable tudp 0 tudp !
2132: : u, ( n -- udp )
2133: tup @ tudp @ + T ! H
1.19 pazsan 2134: tudp @ dup T cell+ H tudp ! ;
1.3 pazsan 2135: : au, ( n -- udp )
2136: tup @ tudp @ + T A! H
1.19 pazsan 2137: tudp @ dup T cell+ H tudp ! ;
1.3 pazsan 2138: >TARGET
2139:
2140: Build: T 0 u, , H ;
1.42 pazsan 2141: by: :douser ( ghost -- up-addr ) T @ H tup @ + ;DO
1.1 anton 2142: Builder User
2143:
1.3 pazsan 2144: Build: T 0 u, , 0 u, drop H ;
2145: by User
1.1 anton 2146: Builder 2User
2147:
1.3 pazsan 2148: Build: T 0 au, , H ;
2149: by User
1.1 anton 2150: Builder AUser
2151:
1.52 jwilke 2152: BuildSmart: T , H ;
1.44 pazsan 2153: by (Constant)
1.1 anton 2154: Builder Value
2155:
1.52 jwilke 2156: BuildSmart: T A, H ;
1.44 pazsan 2157: by (Constant)
1.32 pazsan 2158: Builder AValue
2159:
1.52 jwilke 2160: BuildSmart: ( -- ) [T'] noop T A, H ;
1.42 pazsan 2161: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.1 anton 2162: Builder Defer
1.37 pazsan 2163:
1.52 jwilke 2164: BuildSmart: ( inter comp -- ) swap T immediate A, A, H ;
1.37 pazsan 2165: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.38 anton 2166: Builder interpret/compile:
1.24 pazsan 2167:
2168: \ Sturctures 23feb95py
2169:
2170: >CROSS
2171: : nalign ( addr1 n -- addr2 )
2172: \ addr2 is the aligned version of addr1 wrt the alignment size n
2173: 1- tuck + swap invert and ;
2174: >TARGET
2175:
1.44 pazsan 2176: Build: ;
2177: by: :dofield T @ H + ;DO
2178: Builder (Field)
2179:
1.51 anton 2180: Build: ( align1 offset1 align size "name" -- align2 offset2 )
2181: rot dup T , H ( align1 align size offset1 )
2182: + >r nalign r> ;
1.44 pazsan 2183: by (Field)
1.24 pazsan 2184: Builder Field
2185:
1.51 anton 2186: : struct T 1 chars 0 H ;
1.24 pazsan 2187: : end-struct T 2Constant H ;
2188:
1.52 jwilke 2189: : cell% ( n -- size align )
1.51 anton 2190: T 1 cells H dup ;
1.24 pazsan 2191:
2192: \ ' 2Constant Alias2 end-struct
2193: \ 0 1 T Chars H 2Constant struct
1.1 anton 2194:
1.52 jwilke 2195: \ structural conditionals 17dec92py
2196:
2197: >CROSS
2198: : ?struc ( flag -- ) ABORT" CROSS: unstructured " ;
2199: : sys? ( sys -- sys ) dup 0= ?struc ;
2200: : >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ;
2201:
1.67 jwilke 2202: : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw
1.52 jwilke 2203:
2204: : >resolve ( sys -- ) T here ( dup ." >" hex. ) over branchoffset swap ! H ;
2205:
2206: : <resolve ( sys -- ) T here ( dup ." <" hex. ) branchoffset , H ;
2207:
1.54 pazsan 2208: :noname compile branch T here branchoffset , H ;
2209: IS branch, ( target-addr -- )
2210: :noname compile ?branch T here branchoffset , H ;
2211: IS ?branch, ( target-addr -- )
2212: :noname compile branch T here 0 , H ;
2213: IS branchmark, ( -- branchtoken )
2214: :noname compile ?branch T here 0 , H ;
2215: IS ?branchmark, ( -- branchtoken )
2216: :noname T here 0 , H ;
2217: IS ?domark, ( -- branchtoken )
2218: :noname dup T @ H ?struc T here over branchoffset swap ! H ;
2219: IS branchtoresolve, ( branchtoken -- )
2220: :noname branchto, T here H ;
2221: IS branchtomark, ( -- target-addr )
1.52 jwilke 2222:
2223: >TARGET
2224:
2225: \ Structural Conditionals 12dec92py
2226:
2227: Cond: BUT restrict? sys? swap ;Cond
2228: Cond: YET restrict? sys? dup ;Cond
2229:
1.54 pazsan 2230: 0 [IF]
1.52 jwilke 2231: >CROSS
2232: Variable tleavings
2233: >TARGET
2234:
2235: Cond: DONE ( addr -- ) restrict? tleavings @
2236: BEGIN 2dup u> 0= WHILE dup T @ H swap >resolve REPEAT
2237: tleavings ! drop ;Cond
2238:
2239: >CROSS
1.54 pazsan 2240: : (leave) T here H tleavings @ T , H tleavings ! ;
1.52 jwilke 2241: >TARGET
2242:
1.54 pazsan 2243: Cond: LEAVE restrict? compile branch (leave) ;Cond
2244: Cond: ?LEAVE restrict? compile 0= compile ?branch (leave) ;Cond
1.52 jwilke 2245:
1.53 jwilke 2246: [ELSE]
2247: \ !! This is WIP
2248: \ The problem is (?DO)!
2249: \ perhaps we need a plug-in for (?DO)
2250:
2251: >CROSS
2252: Variable tleavings 0 tleavings !
1.54 pazsan 2253: : (done) ( addr -- )
2254: tleavings @
2255: BEGIN dup
2256: WHILE
2257: >r dup r@ cell+ @ \ address of branch
2258: u> 0= \ lower than DO?
2259: WHILE
2260: r@ 2 cells + @ \ branch token
2261: branchtoresolve,
2262: r@ @ r> free throw
2263: REPEAT r> THEN
2264: tleavings ! drop ;
2265:
1.53 jwilke 2266: >TARGET
2267:
1.54 pazsan 2268: Cond: DONE ( addr -- ) restrict? (done) ;Cond
1.53 jwilke 2269:
2270: >CROSS
1.54 pazsan 2271: : (leave) ( branchtoken -- )
1.53 jwilke 2272: 3 cells allocate throw >r
2273: T here H r@ cell+ !
2274: r@ 2 cells + !
2275: tleavings @ r@ !
2276: r> tleavings ! ;
2277: >TARGET
2278:
1.54 pazsan 2279: Cond: LEAVE restrict? branchmark, (leave) ;Cond
2280: Cond: ?LEAVE restrict? compile 0= ?branchmark, (leave) ;Cond
1.53 jwilke 2281:
2282: [THEN]
2283:
1.54 pazsan 2284: >CROSS
2285: \ !!JW ToDo : Move to general tools section
2286:
2287: : to1 ( x1 x2 xn n -- addr )
2288: \G packs n stack elements in a allocated memory region
2289: dup dup 1+ cells allocate throw dup >r swap 1+
2290: 0 DO tuck ! cell+ LOOP
2291: drop r> ;
2292: : 1to ( addr -- x1 x2 xn )
2293: \G unpacks the elements saved by to1
2294: dup @ swap over cells + swap
2295: 0 DO dup @ swap 1 cells - LOOP
2296: free throw ;
2297:
2298: : loop] branchto, dup <resolve tcell - (done) ;
2299:
2300: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
2301:
2302: >TARGET
2303:
1.52 jwilke 2304: \ Structural Conditionals 12dec92py
2305:
1.53 jwilke 2306: >TARGET
1.52 jwilke 2307: Cond: AHEAD restrict? branchmark, ;Cond
2308: Cond: IF restrict? ?branchmark, ;Cond
2309: Cond: THEN restrict? sys? branchto, branchtoresolve, ;Cond
2310: Cond: ELSE restrict? sys? compile AHEAD swap compile THEN ;Cond
2311:
2312: Cond: BEGIN restrict? branchtomark, ;Cond
2313: Cond: WHILE restrict? sys? compile IF swap ;Cond
2314: Cond: AGAIN restrict? sys? branch, ;Cond
2315: Cond: UNTIL restrict? sys? ?branch, ;Cond
2316: Cond: REPEAT restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
2317:
2318: Cond: CASE restrict? 0 ;Cond
2319: Cond: OF restrict? 1+ >r compile over compile =
2320: compile IF compile drop r> ;Cond
1.45 pazsan 2321: Cond: ENDOF restrict? >r compile ELSE r> ;Cond
2322: Cond: ENDCASE restrict? compile drop 0 ?DO compile THEN LOOP ;Cond
1.1 anton 2323:
2324: \ Structural Conditionals 12dec92py
2325:
1.67 jwilke 2326: :noname \ ?? i think 0 is too much! jaw
1.54 pazsan 2327: 0 compile (do)
2328: branchtomark, 2 to1 ;
2329: IS do, ( -- target-addr )
2330:
2331: \ :noname
2332: \ compile 2dup compile = compile IF
2333: \ compile 2drop compile ELSE
2334: \ compile (do) branchtomark, 2 to1 ;
2335: \ IS ?do,
2336:
2337: :noname
2338: 0 compile (?do) ?domark, (leave)
2339: branchtomark, 2 to1 ;
2340: IS ?do, ( -- target-addr )
2341: :noname compile (for) branchtomark, ;
2342: IS for, ( -- target-addr )
2343: :noname 1to compile (loop) loop] compile unloop skiploop] ;
2344: IS loop, ( target-addr -- )
2345: :noname 1to compile (+loop) loop] compile unloop skiploop] ;
2346: IS +loop, ( target-addr -- )
2347: :noname compile (next) loop] compile unloop ;
2348: IS next, ( target-addr -- )
2349:
2350: Cond: DO restrict? do, ;Cond
2351: Cond: ?DO restrict? ?do, ;Cond
2352: Cond: FOR restrict? for, ;Cond
2353:
2354: Cond: LOOP restrict? sys? loop, ;Cond
2355: Cond: +LOOP restrict? sys? +loop, ;Cond
2356: Cond: NEXT restrict? sys? next, ;Cond
1.52 jwilke 2357:
1.1 anton 2358: \ String words 23feb93py
2359:
1.52 jwilke 2360: : ," [char] " parse T string, align H ;
1.1 anton 2361:
2362: Cond: ." restrict? compile (.") T ," H ;Cond
2363: Cond: S" restrict? compile (S") T ," H ;Cond
2364: Cond: ABORT" restrict? compile (ABORT") T ," H ;Cond
2365:
2366: Cond: IS T ' >body H compile ALiteral compile ! ;Cond
1.66 pazsan 2367: : IS T >address ' >body ! H ;
1.9 pazsan 2368: Cond: TO T ' >body H compile ALiteral compile ! ;Cond
2369: : TO T ' >body ! H ;
1.1 anton 2370:
1.52 jwilke 2371: Cond: defers T ' >body @ compile, H ;Cond
2372: : on T -1 swap ! H ;
2373: : off T 0 swap ! H ;
2374:
1.1 anton 2375: \ LINKED ERR" ENV" 2ENV" 18may93jaw
2376:
2377: \ linked list primitive
2378: : linked T here over @ A, swap ! H ;
1.52 jwilke 2379: : chained T linked A, H ;
1.1 anton 2380:
2381: : err" s" ErrLink linked" evaluate T , H
1.52 jwilke 2382: [char] " parse T string, align H ;
1.1 anton 2383:
2384: : env" [char] " parse s" EnvLink linked" evaluate
1.52 jwilke 2385: T string, align , H ;
1.1 anton 2386:
2387: : 2env" [char] " parse s" EnvLink linked" evaluate
1.52 jwilke 2388: here >r T string, align , , H
1.1 anton 2389: r> dup T c@ H 80 and swap T c! H ;
2390:
2391: \ compile must be last 22feb93py
2392:
2393: Cond: compile ( -- ) restrict? \ name
1.13 pazsan 2394: bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1 anton 2395: 0> IF gexecute
2396: ELSE dup >magic @ <imm> =
2397: IF gexecute
1.54 pazsan 2398: ELSE compile (compile) addr, THEN THEN ;Cond
1.1 anton 2399:
2400: Cond: postpone ( -- ) restrict? \ name
1.13 pazsan 2401: bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1 anton 2402: 0> IF gexecute
2403: ELSE dup >magic @ <imm> =
2404: IF gexecute
1.54 pazsan 2405: ELSE compile (compile) addr, THEN THEN ;Cond
2406:
1.77 ! jwilke 2407: \ save-cross 17mar93py
! 2408:
! 2409: hex
! 2410:
! 2411: >CROSS
! 2412: Create magic s" Gforth2x" here over allot swap move
! 2413:
! 2414: bigendian 1+ \ strangely, in magic big=0, little=1
! 2415: tcell 1 = 0 and or
! 2416: tcell 2 = 2 and or
! 2417: tcell 4 = 4 and or
! 2418: tcell 8 = 6 and or
! 2419: tchar 1 = 00 and or
! 2420: tchar 2 = 28 and or
! 2421: tchar 4 = 50 and or
! 2422: tchar 8 = 78 and or
! 2423: magic 7 + c!
! 2424:
! 2425: : save-cross ( "image-name" "binary-name" -- )
! 2426: bl parse ." Saving to " 2dup type cr
! 2427: w/o bin create-file throw >r
! 2428: TNIL IF
! 2429: s" #! " r@ write-file throw
! 2430: bl parse r@ write-file throw
! 2431: s" -i" r@ write-file throw
! 2432: #lf r@ emit-file throw
! 2433: r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
! 2434: ?do
! 2435: bl over emit-file throw
! 2436: loop
! 2437: drop
! 2438: magic 8 r@ write-file throw \ write magic
! 2439: ELSE
! 2440: bl parse 2drop
! 2441: THEN
! 2442: image @ there
! 2443: r@ write-file throw \ write image
! 2444: TNIL IF
! 2445: bit$ @ there 1- tcell>bit rshift 1+
! 2446: r@ write-file throw \ write tags
! 2447: THEN
! 2448: r> close-file throw ;
! 2449:
! 2450: : save-region ( addr len -- )
! 2451: bl parse w/o bin create-file throw >r
! 2452: swap >image swap r@ write-file throw
! 2453: r> close-file throw ;
! 2454:
1.54 pazsan 2455: \ \ minimal definitions
2456:
1.77 ! jwilke 2457: >MINIMAL also minimal
! 2458:
1.1 anton 2459: \ Usefull words 13feb93py
2460:
2461: : KB 400 * ;
2462:
1.54 pazsan 2463: \ \ [IF] [ELSE] [THEN] ... 14sep97jaw
2464:
2465: \ it is useful to define our own structures and not to rely
2466: \ on the words in the compiler
2467: \ The words in the compiler might be defined with vocabularies
2468: \ this doesn't work with our self-made compile-loop
2469:
2470: Create parsed 20 chars allot \ store word we parsed
2471:
2472: : upcase
2473: parsed count bounds
2474: ?DO I c@ toupper I c! LOOP ;
2475:
2476: : [ELSE]
2477: 1 BEGIN
2478: BEGIN bl word count dup WHILE
1.77 ! jwilke 2479: comment? 20 umin parsed place upcase parsed count
1.54 pazsan 2480: 2dup s" [IF]" compare 0= >r
2481: 2dup s" [IFUNDEF]" compare 0= >r
2482: 2dup s" [IFDEF]" compare 0= r> or r> or
2483: IF 2drop 1+
2484: ELSE 2dup s" [ELSE]" compare 0=
2485: IF 2drop 1- dup
2486: IF 1+
2487: THEN
2488: ELSE
2489: 2dup s" [ENDIF]" compare 0= >r
2490: s" [THEN]" compare 0= r> or
2491: IF 1- THEN
2492: THEN
2493: THEN
2494: ?dup 0= ?EXIT
2495: REPEAT
2496: 2drop refill 0=
2497: UNTIL drop ; immediate
2498:
2499: : [THEN] ( -- ) ; immediate
2500:
2501: : [ENDIF] ( -- ) ; immediate
2502:
2503: : [IF] ( flag -- )
2504: 0= IF postpone [ELSE] THEN ; immediate
2505:
2506: Cond: [IF] postpone [IF] ;Cond
2507: Cond: [THEN] postpone [THEN] ;Cond
2508: Cond: [ELSE] postpone [ELSE] ;Cond
2509:
1.1 anton 2510: \ define new [IFDEF] and [IFUNDEF] 20may93jaw
2511:
1.77 ! jwilke 2512: : defined? tdefined? ;
1.44 pazsan 2513: : needed? needed? ;
2514: : doer? doer? ;
1.1 anton 2515:
1.52 jwilke 2516: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
2517:
2518: : directive?
1.77 ! jwilke 2519: bl word count [ ' target >wordlist ] literal search-wordlist
1.52 jwilke 2520: dup IF nip THEN ;
2521:
2522: : [IFDEF] >in @ directive? swap >in !
1.77 ! jwilke 2523: 0= IF tdefined? ELSE name 2drop true THEN
1.52 jwilke 2524: postpone [IF] ;
2525:
1.77 ! jwilke 2526: : [IFUNDEF] tdefined? 0= postpone [IF] ;
1.1 anton 2527:
1.54 pazsan 2528: Cond: [IFDEF] postpone [IFDEF] ;Cond
2529:
2530: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
2531:
1.1 anton 2532: \ C: \- \+ Conditional Compiling 09jun93jaw
2533:
1.77 ! jwilke 2534: : C: >in @ tdefined? 0=
! 2535: IF >in ! X :
1.1 anton 2536: ELSE drop
2537: BEGIN bl word dup c@
2538: IF count comment? s" ;" compare 0= ?EXIT
2539: ELSE refill 0= ABORT" CROSS: Out of Input while C:"
2540: THEN
2541: AGAIN
2542: THEN ;
2543:
1.74 jwilke 2544: : d? d? ;
2545:
2546: \G doesn't skip line when debug switch is on
2547: : \D D? 0= IF postpone \ THEN ;
1.52 jwilke 2548:
1.48 anton 2549: \G interprets the line if word is not defined
1.77 ! jwilke 2550: : \- tdefined? IF postpone \ THEN ;
1.48 anton 2551:
2552: \G interprets the line if word is defined
1.77 ! jwilke 2553: : \+ tdefined? 0= IF postpone \ THEN ;
1.1 anton 2554:
1.48 anton 2555: Cond: \- \- ;Cond
2556: Cond: \+ \+ ;Cond
1.52 jwilke 2557: Cond: \D \D ;Cond
1.48 anton 2558:
2559: : ?? bl word find IF execute ELSE drop 0 THEN ;
2560:
2561: : needed:
2562: \G defines ghost for words that we want to be compiled
2563: BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
2564:
1.1 anton 2565: \ words that should be in minimal
1.52 jwilke 2566:
2567: create s-buffer 50 chars allot
2568:
1.77 ! jwilke 2569: bigendian Constant bigendian
1.1 anton 2570:
1.52 jwilke 2571: : here there ;
1.67 jwilke 2572: : equ constant ;
2573: : mark there constant ;
1.54 pazsan 2574:
2575: \ compiler directives
1.52 jwilke 2576: : >ram >ram ;
2577: : >rom >rom ;
2578: : >auto >auto ;
2579: : >tempdp >tempdp ;
2580: : tempdp> tempdp> ;
2581: : const constflag on ;
2582: : warnings name 3 = 0= twarnings ! drop ;
1.60 anton 2583: : | ;
2584: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
1.52 jwilke 2585:
1.48 anton 2586: : save-cross save-cross ;
1.52 jwilke 2587: : save-region save-region ;
2588: : tdump swap >image swap dump ;
2589:
1.48 anton 2590: also forth
1.52 jwilke 2591: [IFDEF] Label : Label defempty? Label ; [THEN]
2592: [IFDEF] start-macros : start-macros defempty? start-macros ; [THEN]
1.77 ! jwilke 2593: \ [IFDEF] builttag : builttag builttag ; [THEN]
1.48 anton 2594: previous
2595:
1.52 jwilke 2596: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
1.43 pazsan 2597: : + + ;
1.52 jwilke 2598: : 1+ 1 + ;
2599: : 2+ 2 + ;
1.43 pazsan 2600: : 1- 1- ;
2601: : - - ;
1.52 jwilke 2602: : and and ;
2603: : or or ;
1.43 pazsan 2604: : 2* 2* ;
2605: : * * ;
2606: : / / ;
2607: : dup dup ;
2608: : over over ;
2609: : swap swap ;
2610: : rot rot ;
2611: : drop drop ;
2612: : = = ;
2613: : 0= 0= ;
2614: : lshift lshift ;
2615: : 2/ 2/ ;
1.19 pazsan 2616: : . . ;
1.1 anton 2617:
1.43 pazsan 2618: : all-words ['] false IS skip? ;
2619: : needed-words ['] needed? IS skip? ;
1.75 jwilke 2620: : undef-words ['] defined2? IS skip? ;
2621: : skipdef skipdef ;
1.1 anton 2622:
1.40 pazsan 2623: : \ postpone \ ; immediate
1.47 pazsan 2624: : \G T-\G ; immediate
1.40 pazsan 2625: : ( postpone ( ; immediate
1.1 anton 2626: : include bl word count included ;
1.52 jwilke 2627: : require require ;
1.1 anton 2628: : .( [char] ) parse type ;
1.52 jwilke 2629: : ." [char] " parse type ;
1.1 anton 2630: : cr cr ;
2631:
1.77 ! jwilke 2632: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
! 2633:
! 2634: \ only forth also cross also minimal definitions order
1.1 anton 2635:
2636: \ cross-compiler words
2637:
2638: : decimal decimal ;
2639: : hex hex ;
2640:
1.77 ! jwilke 2641: \ : tudp X tudp ;
! 2642: \ : tup X tup ;
! 2643:
! 2644: : doc-off false to-doc ! ;
! 2645: : doc-on true to-doc ! ;
1.39 pazsan 2646:
1.52 jwilke 2647: [IFDEF] dbg : dbg dbg ; [THEN]
1.39 pazsan 2648:
1.1 anton 2649: \ for debugging...
2650: : order order ;
1.52 jwilke 2651: : hwords words ;
2652: : words also ghosts words previous ;
1.1 anton 2653: : .s .s ;
2654: : bye bye ;
2655:
2656: \ turnkey direction
2657: : H forth ; immediate
2658: : T minimal ; immediate
2659: : G ghosts ; immediate
2660:
1.77 ! jwilke 2661: : turnkey
! 2662: \GFORTH 0 set-order also Target
! 2663: \ANSI [ ' target >wordlist ] Literal 1 set-order
! 2664: definitions
! 2665: also Minimal also ;
1.1 anton 2666:
2667: \ these ones are pefered:
2668:
2669: : lock turnkey ;
1.74 jwilke 2670: : unlock previous forth also cross ;
1.52 jwilke 2671:
1.77 ! jwilke 2672: \ also minimal
1.52 jwilke 2673: : [[ also unlock ;
2674: : ]] previous previous ;
1.1 anton 2675:
2676: unlock definitions also minimal
2677: : lock lock ;
2678: lock
1.77 ! jwilke 2679:
! 2680: \ load cross compiler extension defined in mach file
! 2681:
! 2682: UNLOCK >CROSS
! 2683:
! 2684: [IFDEF] extend-cross extend-cross [THEN]
! 2685:
! 2686: LOCK
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>