--- gforth/cross.fs 2001/09/04 13:06:06 1.103 +++ gforth/cross.fs 2003/09/14 21:16:48 1.142 @@ -1,7 +1,7 @@ \ CROSS.FS The Cross-Compiler 06oct92py \ Idea and implementation: Bernd Paysan (py) -\ Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc. +\ Copyright (C) 1995,1996,1997,1998,1999,2000,2003 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -23,14 +23,15 @@ [IF] ToDo: -Crossdoc destination ./doc/crossdoc.fd makes no sense when -cross.fs is uses seperately. jaw -Do we need this char translation with >address and in branchoffset? -(>body also affected) jaw -Clean up mark> and >resolve stuff jaw +- Crossdoc destination ./doc/crossdoc.fd makes no sense when + cross.fs is used seperately. jaw +- Do we need this char translation with >address and in branchoffset? + (>body also affected) jaw [THEN] +s" compat/strcomp.fs" included + hex \ debugging for compiling @@ -62,6 +63,7 @@ forth definitions : T previous Ghosts also Target ; immediate : G Ghosts ; immediate + : >cross also Cross definitions previous ; : >target also Target definitions previous ; : >minimal also Minimal definitions previous ; @@ -70,6 +72,10 @@ H >CROSS +\ Test against this definitions to find out whether we are cross-compiling +\ may be usefull for assemblers +0 Constant gforth-cross-indicator + \ find out whether we are compiling with gforth : defined? bl word find nip ; @@ -201,6 +207,13 @@ Create bases 10 , 2 , A , 100 , [THEN] +\ this provides assert( and struct stuff +\GFORTH [IFUNDEF] assert1( +\GFORTH also forth definitions require assert.fs previous +\GFORTH [THEN] + +>CROSS + hex \ the defualt base for the cross-compiler is hex !! \ Warnings off @@ -241,16 +254,27 @@ hex \ the defualt base for the cross hex +\ FIXME delete` \ 1 Constant Cross-Flag \ to check whether assembler compiler plug-ins are \ for cross-compiling \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!! +\ FIXME move down : comment? ( c-addr u -- c-addr u ) - 2dup s" (" compare 0= + 2dup s" (" str= IF postpone ( - ELSE 2dup s" \" compare 0= IF postpone \ THEN + ELSE 2dup s" \" str= IF postpone \ THEN THEN ; +: X ( -- ) +\G The next word in the input is a target word. +\G Equivalent to T but without permanent +\G switch to target dictionary. Used as prefix e.g. for @, !, here etc. + bl word count [ ' target >wordlist ] Literal search-wordlist + IF state @ IF compile, ELSE execute THEN + ELSE -1 ABORT" Cross: access method not supported!" + THEN ; immediate + \ Begin CROSS COMPILER: \ debugging @@ -296,6 +320,18 @@ set-order previous \ POSTPONE false THEN ; immediate +: symentry ( adr len taddr -- ) +\G Produce a symbol table (an optional symbol address +\G map) if wanted + [ [IFDEF] fd-symbol-table ] + base @ swap hex s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base ! + s" :" fd-symbol-table write-file throw + fd-symbol-table write-line throw + [ [ELSE] ] + 2drop drop + [ [THEN] ] ; + + \ \ -------------------- source file decimal @@ -440,8 +476,8 @@ sourcepath value fpath 2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/.... over c@ [char] / = >r over c@ [char] ~ = >r - \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic - 2 min S" ./" compare 0= + \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic + S" ./" string-prefix? r> r> r> or or or ; Create ofile 0 c, 255 chars allot @@ -457,14 +493,14 @@ Create tfile 0 c, 255 chars allot REPEAT ; : remove~+ ( -- ) - ofile count 3 min s" ~+/" compare 0= + ofile count s" ~+/" string-prefix? IF ofile count 3 /string ofile place THEN ; : expandtopic ( -- ) \ stack effect correct? - anton \ expands "./" into an absolute name - ofile count 2 min s" ./" compare 0= + ofile count s" ./" string-prefix? IF ofile count 1 /string tfile place 0 ofile c! sourcefilename extractpath ofile place @@ -477,7 +513,7 @@ Create tfile 0 c, 255 chars allot \ deletes phrases like "xy/.." out of our directory name 2dec97jaw over swap BEGIN dup WHILE - dup >r '/ scan 2dup 4 min s" /../" compare 0= + dup >r '/ scan 2dup s" /../" string-prefix? IF dup r> - >r 4 /string over r> + 4 - swap 2dup + >r move dup r> over - @@ -540,7 +576,7 @@ fpath= ~+ : included? ( c-addr u -- f ) file-list BEGIN @ dup - WHILE >r 2dup r@ >fl-name count compare 0= + WHILE >r 2dup r@ >fl-name count str= IF rdrop 2drop true EXIT THEN r> REPEAT @@ -626,16 +662,168 @@ stack-warn [IF] : defempty? empty? ; [ELSE] : defempty? ; immediate +\ : defempty? .sourcepos ; [THEN] +\ \ -------------------- Compiler Plug Ins 01aug97jaw + +>CROSS + +\ Compiler States + +Variable comp-state +0 Constant interpreting +1 Constant compiling +2 Constant resolving +3 Constant assembling + +: compiling? comp-state @ compiling = ; + +: pi-undefined -1 ABORT" Plugin undefined" ; + +: Plugin ( -- : pluginname ) + Create + \ for normal cross-compiling only one action + \ exists, this fields are identical. For the instant + \ simulation environment we need, two actions for each plugin + \ the target one and the one that generates the simulation code + ['] pi-undefined , \ action + ['] pi-undefined , \ target plugin action + 8765 , \ plugin magic + DOES> perform ; + +Plugin DummyPlugin + +: 'PI ( -- addr : pluginname ) + ' >body dup 2 cells + @ 8765 <> ABORT" not a plugin" ; + +: plugin-of ( xt -- : pluginname ) + dup 'PI 2! ; + +: action-of ( xt -- : plunginname ) + 'PI cell+ ! ; + +: TPA ( -- : plugin ) +\ target plugin action +\ executes current target action of plugin + 'PI cell+ POSTPONE literal POSTPONE perform ; immediate + +Variable ppi-temp 0 ppi-temp ! + +: pa: +\g define plugin action + ppi-temp @ ABORT" pa: definition not closed" + 'PI ppi-temp ! :noname ; + +: ;pa +\g end a definition for plugin action + POSTPONE ; ppi-temp @ ! 0 ppi-temp ! ; immediate + + +Plugin dlit, ( d -- ) \ compile numerical value the target +Plugin lit, ( n -- ) +Plugin alit, ( n -- ) + +Plugin branch, ( target-addr -- ) \ compiles a branch +Plugin ?branch, ( target-addr -- ) \ compiles a ?branch +Plugin branchmark, ( -- branch-addr ) \ reserves room for a branch +Plugin ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch +Plugin ?domark, ( -- branch-addr ) \ reserves room for a ?do branch +Plugin branchto, ( -- ) \ actual program position is target of a branch (do e.g. alignment) +' NOOP plugin-of branchto, +Plugin branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark +Plugin branchtomark, ( -- target-addr ) \ marks a branch destination + +Plugin colon, ( tcfa -- ) \ compiles call to tcfa at current position +Plugin prim, ( tcfa -- ) \ compiles primitive invocation +Plugin colonmark, ( -- addr ) \ marks a colon call +Plugin colon-resolve ( tcfa addr -- ) + +Plugin addr-resolve ( target-addr addr -- ) +Plugin doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt ) + +Plugin ncontrols? ( [ xn ... x1 ] n -- ) \ checks wheter n control structures are open +Plugin if, ( -- if-token ) +Plugin else, ( if-token -- if-token ) +Plugin then, ( if-token -- ) +Plugin ahead, +Plugin begin, +Plugin while, +Plugin until, +Plugin again, +Plugin repeat, +Plugin cs-swap ( x1 x2 -- x2 x1 ) + +Plugin case, ( -- n ) +Plugin of, ( n -- x1 n ) +Plugin endof, ( x1 n -- x2 n ) +Plugin endcase, ( x1 .. xn n -- ) + +Plugin do, ( -- do-token ) +Plugin ?do, ( -- ?do-token ) +Plugin for, ( -- for-token ) +Plugin loop, ( do-token / ?do-token -- ) +Plugin +loop, ( do-token / ?do-token -- ) +Plugin next, ( for-token ) +Plugin leave, ( -- ) +Plugin ?leave, ( -- ) + +Plugin ca>native \ Convert a code address to the processors + \ native address. This is used in doprim, and + \ code/code: primitive definitions word to + \ convert the addresses. + \ The only target where we need this is the misc + \ which is a 16 Bit processor with word addresses + \ but the forth system we build has a normal byte + \ addressed memory model + +Plugin doprim, \ compiles start of a primitive +Plugin docol, \ compiles start of a colon definition +Plugin doer, +Plugin fini, \ compiles end of definition ;s +Plugin doeshandler, +Plugin dodoes, + +Plugin colon-start +' noop plugin-of colon-start +Plugin colon-end +' noop plugin-of colon-end + +Plugin ]comp \ starts compilation +' noop plugin-of ]comp +Plugin comp[ \ ends compilation +' noop plugin-of comp[ + +Plugin t>body \ we need the system >body + \ and the target >body + +>TARGET +: >body t>body ; + + \ Ghost Builder 06oct92py +>CROSS hex +\ Values for ghost magic 4711 Constant 4712 Constant 4713 Constant 4714 Constant 4715 Constant +\ Bitmask for ghost flags 1 Constant +2 Constant + +\ FXIME: move this to general stuff? +: set-flag ( addr flag -- ) + over @ or swap ! ; + +: reset-flag ( addr flag -- ) + invert over @ and swap ! ; + +: get-flag ( addr flag -- f ) + swap @ and 0<> ; + Struct @@ -649,11 +837,25 @@ Struct \ points to the where we have to resolve (linked-list) cell% field >link - \ execution symantics (while target compiling) of ghost + \ execution semantics (while target compiling) of ghost cell% field >exec + \ compilation action of this ghost; this is what is + \ done to compile a call (or whatever) to this definition. + \ E.g. >comp contains the semantic of postpone s" + \ whereas >exec-compile contains the semantic of s" + cell% field >comp + + \ Compilation sematics (while parsing) of this ghost. E.g. + \ "\" will skip the rest of line. + \ These semantics are defined by Cond: and + \ if a word is made immediate in instant, then the >exec2 field + \ gets copied to here cell% field >exec-compile + \ Additional execution semantics of this ghost. This is used + \ for code generated by instant and for the doer-xt of created + \ words cell% field >exec2 cell% field >created @@ -711,6 +913,7 @@ Variable cross-space-dp-orig cross-space-end u> ABORT" CROSS: cross-space overflow" cross-space-dp-orig @ dp ! ; +\ this is just for debugging, to see this in the backtrace : execute-exec execute ; : execute-exec2 execute ; : execute-exec-compile execute ; @@ -722,8 +925,11 @@ Variable cross-space-dp-orig ELSE true ABORT" CROSS: Don't execute ghost, or immediate target word" THEN ; +Defer is-forward + : (ghostheader) ( -- ) - ghost-list linked , 0 , ['] NoExec , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ; + ghost-list linked , 0 , ['] NoExec , ['] is-forward , + 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ; : ghostheader ( -- ) (ghostheader) 0 , ; @@ -783,6 +989,9 @@ Defer search-ghosts REPEAT drop r> false ; +: xt>ghost ( xt -- ghost ) + gdiscover 0= ABORT" CROSS: ghost not found for this xt" ; + : Ghost ( "name" -- ghost ) >in @ bl word gfind IF nip EXIT THEN drop >in ! Make-Ghost ; @@ -812,7 +1021,6 @@ Exists-Warnings on Variable reuse-ghosts reuse-ghosts off -1 [IF] \ FIXME: define when vocs are ready : HeaderGhost ( "name" -- ghost ) >in @ bl word count @@ -829,21 +1037,28 @@ Variable reuse-ghosts reuse-ghosts off \ defined words, this is a workaround \ for the redefined \ until vocs work Make-Ghost ; -[THEN] - : .ghost ( ghost -- ) >ghostname type ; \ ' >ghostname ALIAS @name +: findghost ( "ghostname" -- ghost ) + bl word gfind 0= ABORT" CROSS: Ghost don't exists" ; + : [G'] ( -- ghost : name ) \G ticks a ghost and returns its address -\ bl word gfind 0= ABORT" CROSS: Ghost don't exists" - ghost state @ IF postpone literal THEN ; immediate + findghost + state @ IF postpone literal THEN ; immediate -: ghost>cfa ( ghost -- cfa ) +: g>xt ( ghost -- xt ) +\G Returns the xt (cfa) of a ghost. Issues a warning if undefined. dup undefined? ABORT" CROSS: forward " >link @ ; +: g>body ( ghost -- body ) +\G Returns the body-address (pfa) of a ghost. +\G Issues a warning if undefined (a forward-reference). + g>xt X >body ; + 1 Constant