--- gforth/cross.fs 1995/07/25 15:28:04 1.26 +++ gforth/cross.fs 1997/07/31 16:17:07 1.51 @@ -1,7 +1,23 @@ \ CROSS.FS The Cross-Compiler 06oct92py -\ $Id: cross.fs,v 1.26 1995/07/25 15:28:04 pazsan Exp $ \ Idea and implementation: Bernd Paysan (py) -\ Copyright 1992-94 by the GNU Forth Development Group + +\ Copyright (C) 1995 Free Software Foundation, Inc. + +\ This file is part of Gforth. + +\ Gforth is free software; you can redistribute it and/or +\ modify it under the terms of the GNU General Public License +\ as published by the Free Software Foundation; either version 2 +\ of the License, or (at your option) any later version. + +\ This program is distributed in the hope that it will be useful, +\ but WITHOUT ANY WARRANTY; without even the implied warranty of +\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\ GNU General Public License for more details. + +\ You should have received a copy of the GNU General Public License +\ along with this program; if not, write to the Free Software +\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. \ Log: \ changed in ; [ to state off 12may93jaw @@ -21,6 +37,11 @@ \ targets 09jun93jaw \ added: 2user and value 11jun93jaw +\ needed? works better now!!! 01mar97jaw +\ mach file is only loaded into target +\ cell corrected + + \ include other.fs \ ansforth extentions for cross : string, ( c-addr u -- ) @@ -75,24 +96,26 @@ H >CROSS -\ Variables 06oct92py +\ Parameter for target systems 06oct92py --1 Constant NIL -Variable image -Variable tlast NIL tlast ! \ Last name field -Variable tlastcfa \ Last code field -Variable tdoes \ Resolve does> calls -Variable bit$ -Variable tdp -: there tdp @ ; +>TARGET +mach-file count included -\ Parameter for target systems 06oct92py +[IFUNDEF] has-interpreter true CONSTANT has-interpreter [THEN] + +also Forth definitions -included +[IFDEF] asm-include asm-include [THEN] + +previous +hex + +>CROSS \ Create additional parameters 19jan95py T +NIL Constant TNIL cell Constant tcell cell<< Constant tcell<< cell>bit Constant tcell>bit @@ -101,6 +124,17 @@ float Constant tfloat 1 bits/byte lshift Constant maxbyte H +\ Variables 06oct92py + +Variable image +Variable tlast TNIL tlast ! \ Last name field +Variable tlastcfa \ Last code field +Variable tdoes \ Resolve does> calls +Variable bit$ +Variable tdp +: there tdp @ ; + + >TARGET \ Byte ordering and cell size 06oct92py @@ -108,21 +142,14 @@ H : cell+ tcell + ; : cells tcell<< lshift ; : chars ; +: char+ 1 + ; : floats tfloat * ; >CROSS : cell/ tcell<< rshift ; >TARGET 20 CONSTANT bl --1 Constant NIL --2 Constant :docol --3 Constant :docon --4 Constant :dovar --5 Constant :douser --6 Constant :dodefer --7 Constant :dostruc --8 Constant :dodoes --9 Constant :doesjump +TNIL Constant NIL >CROSS @@ -157,8 +184,8 @@ bigendian \ MakeKernal 12dec92py >MINIMAL -: makekernal ( targetsize -- targetsize ) - bit$ over 1- cell>bit rshift 1+ initmem +: makekernel ( targetsize -- targetsize ) + bit$ over 1- tcell>bit rshift 1+ initmem image over initmem tdp off ; >CROSS @@ -176,17 +203,17 @@ CREATE Bittable 80 c, 40 c, 20 c, 10 c, \ Target memory access 06oct92py : align+ ( taddr -- rest ) - cell tuck 1- and - [ cell 1- ] Literal and ; + tcell tuck 1- and - [ tcell 1- ] Literal and ; : cfalign+ ( taddr -- rest ) - \ see kernal.fs:cfaligned - float tuck 1- and - [ float 1- ] Literal and ; + \ see kernel.fs:cfaligned + /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ; >TARGET : aligned ( taddr -- ta-addr ) dup align+ + ; \ assumes cell alignment granularity (as GNU C) : cfaligned ( taddr1 -- taddr2 ) - \ see kernal.fs + \ see kernel.fs dup cfalign+ + ; >CROSS @@ -204,7 +231,7 @@ CREATE Bittable 80 c, 40 c, 20 c, 10 c, : here ( -- there ) there ; : allot ( n -- ) tdp +! ; -: , ( w -- ) T here H cell T allot ! H ; +: , ( w -- ) T here H tcell T allot ! H ; : c, ( char -- ) T here 1 allot c! H ; : align ( -- ) T here H align+ 0 ?DO bl T c, H LOOP ; : cfalign ( -- ) @@ -217,15 +244,10 @@ CREATE Bittable 80 c, 40 c, 20 c, 10 c, \ threading modell 13dec92py -\ generic threading modell -: docol, ( -- ) :docol T A, 0 , H ; - >TARGET : >body ( cfa -- pfa ) T cell+ cell+ H ; >CROSS -: dodoes, ( -- ) T :doesjump A, 0 , H ; - \ Ghost Builder 06oct92py \ new version with temp variable 10may93jaw @@ -235,8 +257,9 @@ VARIABLE VocTemp : previous VocTemp @ set-current ; +hex 4711 Constant 4712 Constant -4713 Constant +4713 Constant 4714 Constant \ iForth makes only immediate directly after create \ make atonce trick! ? @@ -247,7 +270,9 @@ Variable atonce atonce off : GhostHeader , 0 , ['] NoExec , ; -: >magic ; : >link cell+ ; : >exec cell+ cell+ ; +: >magic ; +: >link cell+ ; +: >exec cell+ cell+ ; : >end 3 cells + ; Variable last-ghost @@ -257,14 +282,18 @@ Variable last-ghost here tuck swap ! ghostheader T> DOES> dup last-ghost ! >exec @ execute ; +variable cfalist 0 cfalist ! + +: markcfa + cfalist here over @ , swap ! , ; + \ ghost words 14oct92py \ changed: 10may93py/jaw : gfind ( string -- ghost true/1 / string false ) \ searches for string in word-list ghosts -\ !! wouldn't it be simpler to just use search-wordlist ? ae dup count [ ' ghosts >body ] ALiteral search-wordlist - dup IF >r >body nip r> THEN ; + dup IF >r >body nip r> THEN ; VARIABLE Already @@ -327,7 +356,7 @@ variable ResolveFlag WHILE dup ?resolved REPEAT drop ResolveFlag @ IF - abort" Unresolved words!" + -1 abort" Unresolved words!" ELSE ." Nothing!" THEN @@ -341,11 +370,11 @@ variable ResolveFlag VARIABLE ^imm >TARGET -: immediate 20 flag! +: immediate 40 flag! ^imm @ @ dup = IF drop EXIT THEN <> ABORT" CROSS: Cannot immediate a unresolved word" ^imm @ ! ; -: restrict 40 flag! ; +: restrict 20 flag! ; >CROSS \ ALIAS2 ansforth conform alias 9may93jaw @@ -357,20 +386,20 @@ VARIABLE ^imm \ Target Header Creation 01nov92py : string, ( addr count -- ) - dup T c, H bounds DO I c@ T c, H LOOP ; + dup T c, H bounds ?DO I c@ T c, H LOOP ; : name, ( "name" -- ) bl word count string, T cfalign H ; : view, ( -- ) ( dummy ) ; \ Target Document Creation (goes to crossdoc.fd) 05jul95py -s" crossdoc.fd" r/w create-file throw value doc-file-id +s" doc/crossdoc.fd" r/w create-file throw value doc-file-id \ contains the file-id of the documentation file -: \G ( -- ) +: T-\G ( -- ) source >in @ /string doc-file-id write-line throw - source >in ! drop ; immediate + postpone \ ; -Variable to-doc +Variable to-doc to-doc on : cross-doc-entry ( -- ) to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header @@ -383,13 +412,77 @@ Variable to-doc [char] ) parse doc-file-id write-file throw s" )" doc-file-id write-file throw [char] \ parse 2drop - POSTPONE \g + T-\G >in ! - THEN to-doc on ; + THEN ; + +\ Target TAGS creation + +s" kernel.TAGS" r/w create-file throw value tag-file-id +\ contains the file-id of the tags file + +Create tag-beg 2 c, 7F c, bl c, +Create tag-end 2 c, bl c, 01 c, +Create tag-bof 1 c, 0C c, + +2variable last-loadfilename 0 0 last-loadfilename 2! + +: put-load-file-name ( -- ) + loadfilename 2@ last-loadfilename 2@ d<> + IF + tag-bof count tag-file-id write-line throw + sourcefilename 2dup + tag-file-id write-file throw + last-loadfilename 2! + s" ,0" tag-file-id write-line throw + THEN ; + +: cross-tag-entry ( -- ) + tlast @ 0<> \ not an anonymous (i.e. noname) header + IF + put-load-file-name + source >in @ min tag-file-id write-file throw + tag-beg count tag-file-id write-file throw + tlast @ >image count $1F and tag-file-id write-file throw + tag-end count tag-file-id write-file throw + base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw +\ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw + s" ,0" tag-file-id write-line throw + base ! + THEN ; + +\ Check for words + +Defer skip? ' false IS skip? + +: defined? ( -- flag ) \ name + ghost >magic @ <> ; + +: needed? ( -- flag ) \ name +\G returns a false flag when +\G a word is not defined +\G a forward reference exists +\G so the definition is not skipped! + bl word gfind + IF dup >magic @ = + \ swap >link @ 0<> and + nip + 0= + ELSE drop true THEN ; + +: doer? ( -- flag ) \ name + ghost >magic @ = ; + +: skip-defs ( -- ) + BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ; + +\ Target header creation VARIABLE CreateFlag CreateFlag off -: (Theader ( "name" -- ghost ) T align H view, +: (Theader ( "name" -- ghost ) +\ >in @ bl word count type 2 spaces >in ! + T align H view, tlast @ dup 0> IF T 1 cells - THEN A, H there tlast ! >in @ name, >in ! T here H tlastcfa ! CreateFlag @ IF @@ -402,7 +495,7 @@ VARIABLE CreateFlag CreateFlag off Already @ IF dup >end tdoes ! ELSE 0 tdoes ! THEN 80 flag! - cross-doc-entry ; + cross-doc-entry cross-tag-entry ; VARIABLE ;Resolve 1 cells allot @@ -411,8 +504,19 @@ VARIABLE ;Resolve 1 cells allot >TARGET : Alias ( cfa -- ) \ name - dup 0< IF to-doc off THEN - (THeader over resolve T A, H 80 flag! ; + >in @ skip? IF 2drop EXIT THEN >in ! + dup 0< has-prims 0= and + IF + ." needs prim: " >in @ bl word count type >in ! cr + THEN + (THeader over resolve T A, H 80 flag! ; +: Alias: ( cfa -- ) \ name + >in @ skip? IF 2drop EXIT THEN >in ! + dup 0< has-prims 0= and + IF + ." needs doer: " >in @ bl word count type >in ! cr + THEN + ghost tuck swap resolve swap >magic ! ; >CROSS \ Conditionals and Comments 11may93jaw @@ -446,9 +550,11 @@ ghost (loop) ghost (+loop) ghost (next) drop ghost unloop ghost ;S 2drop ghost lit ghost (compile) ghost ! 2drop drop -ghost (;code) ghost noop 2drop +ghost (does>) ghost noop 2drop ghost (.") ghost (S") ghost (ABORT") 2drop drop -ghost ' +ghost ' drop +ghost :docol ghost :doesjump ghost :dodoes 2drop drop +ghost over ghost = ghost drop 2drop drop \ compile 10may93jaw @@ -460,7 +566,37 @@ ghost ' ELSE postpone literal postpone gexecute THEN ; immediate +\ generic threading modell +: docol, ( -- ) compile :docol T 0 , H ; + +: dodoes, ( -- ) T cfalign H compile :doesjump T 0 , H ; + +[IFUNDEF] (code) +Defer (code) +Defer (end-code) +[THEN] + +[IFUNDEF] ca>native +defer ca>native +[THEN] + >TARGET +: Code + (THeader there resolve + [ has-prims 0= [IF] ITC [ELSE] true [THEN] ] [IF] + there 2 T cells H + ca>native T a, 0 , H + [THEN] + depth (code) ; + +: Code: + ghost dup there ca>native resolve swap >magic ! + depth (code) ; + +: end-code + depth ?dup IF 1- <> ABORT" CROSS: Stack changed" + ELSE true ABORT" CROSS: Stack empty" THEN + (end-code) ; + : ' ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined " dup >magic @ = ABORT" CROSS: forward " >link @ ; @@ -475,12 +611,38 @@ Cond: chars ;Cond : alit, ( n -- ) compile lit T A, H ; >TARGET +Cond: \G T-\G ;Cond + Cond: Literal ( n -- ) restrict? lit, ;Cond Cond: ALiteral ( n -- ) restrict? alit, ;Cond : Char ( "" -- ) bl word char+ c@ ; Cond: [Char] ( "" -- ) restrict? Char lit, ;Cond +\ some special literals 27jan97jaw + +Cond: MAXU + restrict? compile lit + tcell 0 ?DO FF T c, H LOOP ;Cond + +Cond: MINI + restrict? compile lit + bigendian IF + 80 T c, H tcell 1 ?DO 0 T c, H LOOP + ELSE + tcell 1 ?DO 0 T c, H LOOP 80 T c, H + THEN + ;Cond + +Cond: MAXI + restrict? compile lit + bigendian IF + 7F T c, H tcell 1 ?DO FF T c, H LOOP + ELSE + tcell 1 ?DO FF T c, H LOOP 7F T c, H + THEN + ;Cond + >CROSS \ Target compiling loop 12dec92py \ ">tib trick thrown out 10may93jaw @@ -513,9 +675,13 @@ Cond: [Char] ( "" -- ) restrict \ is not allowed if a system should be ans conform : : ( -- colon-sys ) \ Name + >in @ skip? IF drop skip-defs EXIT THEN >in ! (THeader ;Resolve ! there ;Resolve cell+ ! docol, depth T ] H ; +: :noname ( -- colon-sys ) + T cfalign H there docol, depth T ] H ; + Cond: EXIT ( -- ) restrict? compile ;S ;Cond Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond @@ -530,11 +696,12 @@ Cond: ; ( -- ) restrict? Cond: [ restrict? state off ;Cond >CROSS -: !does :dodoes tlastcfa @ tuck T ! cell+ ! H ; +: !does + tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ; >TARGET Cond: DOES> restrict? - compile (;code) dodoes, tdoes @ ?dup IF @ T here H resolve THEN + compile (does>) dodoes, tdoes @ ?dup IF @ T here H resolve THEN ;Cond : DOES> dodoes, T here H !does depth T ] H ; @@ -551,8 +718,11 @@ Cond: DOES> restrict? \ DOES> dup >exec @ execute ; : gdoes, ( ghost -- ) >end @ dup >magic @ <> - IF dup >link @ dup 0< IF T A, 0 , H drop EXIT THEN drop THEN - :dodoes T A, H gexecute T here H cell - reloff ; + IF + dup >magic @ = + IF gexecute T 0 , H EXIT THEN + THEN + compile :dodoes gexecute T here H tcell - reloff ; : TCreate ( -- ) last-ghost @ @@ -575,6 +745,10 @@ Cond: DOES> restrict? here ghostheader :noname postpone gdoes> postpone ?EXIT ; +: by: ( -- addr [xt] [colon-sys] ) \ name + ghost + :noname postpone gdoes> postpone ?EXIT ; + : ;DO ( addr [xt] [colon-sys] -- ) postpone ; ( S addr xt ) over >exec ! ; immediate @@ -586,9 +760,8 @@ Cond: DOES> restrict? \ Variables and Constants 05dec92py Build: ; -DO: ( ghost -- addr ) ;DO +by: :dovar ( ghost -- addr ) ;DO Builder Create -by Create :dovar resolve Build: T 0 , H ; by Create @@ -612,9 +785,8 @@ Variable tudp 0 tudp ! >TARGET Build: T 0 u, , H ; -DO: ( ghost -- up-addr ) T @ H tup @ + ;DO +by: :douser ( ghost -- up-addr ) T @ H tup @ + ;DO Builder User -by User :douser resolve Build: T 0 u, , 0 u, drop H ; by User @@ -624,27 +796,37 @@ Build: T 0 au, , H ; by User Builder AUser +Build: ( n -- ) ; +by: :docon ( ghost -- n ) T @ H ;DO +Builder (Constant) + Build: ( n -- ) T , H ; -DO: ( ghost -- n ) T @ H ;DO +by (Constant) Builder Constant -by Constant :docon resolve Build: ( n -- ) T A, H ; -by Constant +by (Constant) Builder AConstant Build: ( d -- ) T , , H ; DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO Builder 2Constant -Build: T 0 , H ; -by Constant +Build: T , H ; +by (Constant) Builder Value +Build: T A, H ; +by (Constant) +Builder AValue + Build: ( -- ) compile noop ; -DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO +by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO Builder Defer -by Defer :dodefer resolve + +Build: ( inter comp -- ) swap T immediate A, A, H ; +DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO +Builder interpret/compile: \ Sturctures 23feb95py @@ -654,17 +836,21 @@ by Defer :dodefer resolve 1- tuck + swap invert and ; >TARGET -Build: >r rot r@ nalign dup T , H ( align1 size offset ) - + swap r> nalign ; -DO: T @ H + ;DO +Build: ; +by: :dofield T @ H + ;DO +Builder (Field) + +Build: ( align1 offset1 align size "name" -- align2 offset2 ) + rot dup T , H ( align1 align size offset1 ) + + >r nalign r> ; +by (Field) Builder Field -by Field :dostruc resolve -: struct T 0 1 chars H ; +: struct T 1 chars 0 H ; : end-struct T 2Constant H ; -: cells: ( n -- size align ) - T cells 1 cells H ; +: cell% ( -- align size ) + T 1 cells H dup ; \ ' 2Constant Alias2 end-struct \ 0 1 T Chars H 2Constant struct @@ -712,14 +898,20 @@ Cond: AGAIN restrict? sys? compile b Cond: UNTIL restrict? sys? compile ?branch r compile over compile = compile IF compile drop + r> ;Cond +Cond: ENDOF restrict? >r compile ELSE r> ;Cond +Cond: ENDCASE restrict? compile drop 0 ?DO compile THEN LOOP ;Cond + \ Structural Conditionals 12dec92py Cond: DO restrict? compile (do) T here H ;Cond -Cond: ?DO restrict? compile (?do) (leave T here H ;Cond +Cond: ?DO restrict? compile (?do) T (leave here H ;Cond Cond: FOR restrict? compile (for) T here H ;Cond >CROSS -: loop] dup TARGET Cond: LOOP restrict? sys? compile (loop) loop] ;Cond @@ -778,14 +970,16 @@ also minimal \ define new [IFDEF] and [IFUNDEF] 20may93jaw -: there? bl word gfind IF >magic @ <> ELSE drop false THEN ; +: defined? defined? ; +: needed? needed? ; +: doer? doer? ; -: [IFDEF] there? postpone [IF] ; -: [IFUNDEF] there? 0= postpone [IF] ; +: [IFDEF] defined? postpone [IF] ; +: [IFUNDEF] defined? 0= postpone [IF] ; \ C: \- \+ Conditional Compiling 09jun93jaw -: C: >in @ there? 0= +: C: >in @ defined? 0= IF >in ! T : H ELSE drop BEGIN bl word dup c@ @@ -797,8 +991,20 @@ also minimal also minimal -: \- there? IF postpone \ THEN ; -: \+ there? 0= IF postpone \ THEN ; +\G interprets the line if word is not defined +: \- defined? IF postpone \ THEN ; + +\G interprets the line if word is defined +: \+ defined? 0= IF postpone \ THEN ; + +Cond: \- \- ;Cond +Cond: \+ \+ ;Cond + +: ?? bl word find IF execute ELSE drop 0 THEN ; + +: needed: +\G defines ghost for words that we want to be compiled + BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ; : [IF] postpone [IF] ; : [THEN] postpone [THEN] ; @@ -810,47 +1016,79 @@ Cond: [IFUNDEF] [IFUNDEF] ;Cond Cond: [THEN] [THEN] ;Cond Cond: [ELSE] [ELSE] ;Cond -\ save-cross 17mar93py +previous -\ i'm not interested in bigforth features this time 10may93jaw -\ [IFDEF] file -\ also file -\ [THEN] -\ included throw after create-file 11may93jaw - -bigendian Constant bigendian +\ save-cross 17mar93py -Create magic s" gforth00" here over allot swap move +>CROSS +Create magic s" Gforth10" here over allot swap move -[char] 1 bigendian + cell + magic 7 + c! +char 1 bigendian + tcell + magic 7 + c! -: save-cross ( "name" -- ) - bl parse ." Saving to " 2dup type +: save-cross ( "image-name" "binary-name" -- ) + bl parse ." Saving to " 2dup type cr w/o bin create-file throw >r - magic 8 r@ write-file throw \ write magic + TNIL IF + s" #! " r@ write-file throw + bl parse r@ write-file throw + s" -i" r@ write-file throw + #lf r@ emit-file throw + r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index ) + ?do + bl over emit-file throw + loop + drop + magic 8 r@ write-file throw \ write magic + ELSE + bl parse 2drop + THEN image @ there r@ write-file throw \ write image - bit$ @ there 1- cell>bit rshift 1+ + TNIL IF + bit$ @ there 1- tcell>bit rshift 1+ r@ write-file throw \ write tags + THEN r> close-file throw ; \ words that should be in minimal +>MINIMAL +also minimal -: + + ; : 1- 1- ; -: - - ; : 2* 2* ; -: * * ; : / / ; -: dup dup ; : over over ; -: swap swap ; : rot rot ; -: drop drop ; : = = ; -: lshift lshift ; : 2/ 2/ ; +bigendian Constant bigendian +: save-cross save-cross ; +: here there ; +also forth +[IFDEF] Label : Label Label ; [THEN] +[IFDEF] start-macros : start-macros start-macros ; [THEN] +previous + +: + + ; +: or or ; +: 1- 1- ; +: - - ; +: 2* 2* ; +: * * ; +: / / ; +: dup dup ; +: over over ; +: swap swap ; +: rot rot ; +: drop drop ; +: = = ; +: 0= 0= ; +: lshift lshift ; +: 2/ 2/ ; : . . ; -cell constant cell +: const ; + +\ mach-file count included -\ include bug5.fs -\ only forth also minimal definitions +: all-words ['] false IS skip? ; +: needed-words ['] needed? IS skip? ; +: undef-words ['] defined? IS skip? ; -: \ postpone \ ; -: \G postpone \G ; -: ( postpone ( ; +: \ postpone \ ; immediate +: \G T-\G ; immediate +: ( postpone ( ; immediate : include bl word count included ; : .( [char] ) parse type ; : cr cr ; @@ -864,7 +1102,12 @@ only forth also minimal definitions : hex hex ; : tudp T tudp H ; -: tup T tup H ; minimal +: tup T tup H ; + +: doc-off false T to-doc H ! ; +: doc-on true T to-doc H ! ; + +minimal \ for debugging... : order order ;