--- gforth/cross.fs 2004/12/01 10:02:59 1.147 +++ gforth/cross.fs 2012/02/13 22:58:13 1.180 @@ -1,13 +1,13 @@ \ CROSS.FS The Cross-Compiler 06oct92py \ Idea and implementation: Bernd Paysan (py) -\ Copyright (C) 1995,1996,1997,1998,1999,2000,2003 Free Software Foundation, Inc. +\ Copyright (C) 1995,1996,1997,1998,1999,2000,2003,2004,2005,2006,2007,2009,2010,2011 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 +\ as published by the Free Software Foundation, either version 3 \ of the License, or (at your option) any later version. \ This program is distributed in the hope that it will be useful, @@ -16,8 +16,7 @@ \ 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA. +\ along with this program. If not, see http://www.gnu.org/licenses/. 0 [IF] @@ -192,7 +191,7 @@ Create bases 10 , 2 , A , 100 , 1+ THEN ; -: number? ( string -- string 0 / n -1 / d 0> ) +: (number?) ( string -- string 0 / n -1 / d 0> ) dup >r count snumber? dup if rdrop else @@ -200,13 +199,15 @@ Create bases 10 , 2 , A , 100 , then ; : number ( string -- d ) - number? ?dup 0= abort" ?" 0< + (number?) ?dup 0= abort" ?" 0< IF s>d THEN ; [THEN] +[IFUNDEF] (number?) : (number?) number? ; [THEN] + \ this provides assert( and struct stuff \GFORTH [IFUNDEF] assert1( \GFORTH also forth definitions require assert.fs previous @@ -764,6 +765,7 @@ Plugin ?do, ( -- ?do-token ) Plugin for, ( -- for-token ) Plugin loop, ( do-token / ?do-token -- ) Plugin +loop, ( do-token / ?do-token -- ) +Plugin -loop, ( do-token / ?do-token -- ) Plugin next, ( for-token ) Plugin leave, ( -- ) Plugin ?leave, ( -- ) @@ -1175,6 +1177,7 @@ false DefaultValue header false DefaultValue backtrace false DefaultValue new-input false DefaultValue peephole +false DefaultValue primcentric false DefaultValue abranch true DefaultValue f83headerstring true DefaultValue control-rack @@ -1184,6 +1187,7 @@ true DefaultValue gforthcross true DefaultValue interpreter true DefaultValue ITC false DefaultValue rom +false DefaultValue flash true DefaultValue standardthreading \ ANSForth environment stuff @@ -1242,6 +1246,9 @@ bits/byte Constant tbits/byte H tbits/char bits/byte / Constant tbyte +: >signed ( u -- n ) + 1 tbits/char tcell * 1- lshift 2dup and + IF negate or ELSE drop THEN ; \ Variables 06oct92py @@ -1720,9 +1727,9 @@ T has? relocate H Ghost (do) Ghost (?do) 2drop Ghost (for) drop -Ghost (loop) Ghost (+loop) 2drop +Ghost (loop) Ghost (+loop) Ghost (-loop) 2drop drop Ghost (next) drop -Ghost (does>) Ghost (does>1) Ghost (does>2) 2drop drop +Ghost !does drop Ghost compile, drop Ghost (.") Ghost (S") Ghost (ABORT") 2drop drop Ghost (C") Ghost c(abort") Ghost type 2drop drop @@ -1751,7 +1758,11 @@ Ghost state drop swap -rot bounds ?DO I c@ over X c! X char+ LOOP drop ; 2Variable last-string +X has? rom [IF] $60 [ELSE] $00 [THEN] Constant header-masks +: ht-header, ( addr count -- ) + dup there swap last-string 2! + dup header-masks or T c, H bounds ?DO I c@ T c, H LOOP ; : ht-string, ( addr count -- ) dup there swap last-string 2! dup T c, H bounds ?DO I c@ T c, H LOOP ; @@ -1762,7 +1773,7 @@ Ghost state drop : count dup X c@ swap X char+ swap ; -: on -1 -1 rot TD! ; +: on >r -1 -1 r> TD! ; : off T 0 swap ! H ; : tcmove ( source dest len -- ) @@ -1771,6 +1782,9 @@ Ghost state drop ?DO dup T c@ H I T c! H 1+ tchar +LOOP drop ; +: tcallot ( char size -- ) + 0 ?DO dup T c, H tchar +LOOP drop ; + : td, ( d -- ) \G Store a host value as one cell into the target there tcell X allot TD! ; @@ -2057,7 +2071,7 @@ $20 constant restrict-mask >TARGET X has? f83headerstring [IF] -: name, ( "name" -- ) bl word count ht-string, X cfalign ; +: name, ( "name" -- ) bl word count ht-header, X cfalign ; [ELSE] : name, ( "name" -- ) bl word count ht-lstring, X cfalign ; [THEN] @@ -2307,7 +2321,13 @@ Variable prim# prim# @ (THeader ( S xt ghost ) ['] prim-resolved over >comp ! dup >ghost-flags set-flag - over resolve-noforwards T A, H alias-mask flag! + s" EC" T $has? H 0= + IF + over resolve-noforwards T A, H + alias-mask flag! + ELSE + T here H resolve-noforwards T A, H + THEN -1 prim# +! ; >CROSS @@ -2393,14 +2413,11 @@ T 2 cells H Value xt>body there xt>body + ca>native T a, H 1 fillcfa ; ' (doprim,) plugin-of doprim, : (doeshandler,) ( -- ) - T cfalign H [G'] :doesjump addr, T 0 , H ; ' (doeshandler,) plugin-of doeshandler, + T H ; ' (doeshandler,) plugin-of doeshandler, : (dodoes,) ( does-action-ghost -- ) ]comp [G'] :dodoes addr, comp[ addr, - \ the relocator in the c engine, does not like the - \ does-address to marked for relocation - [ T e? ec H 0= [IF] ] T here H tcell - reloff [ [THEN] ] 2 fillcfa ; ' (dodoes,) plugin-of dodoes, : (dlit,) ( n -- ) compile lit td, ; ' (dlit,) plugin-of dlit, @@ -2518,7 +2535,7 @@ Cond: MAXI IF nip execute-exec-compile ELSE gexecute THEN EXIT THEN - number? dup + (number?) dup IF 0> IF swap lit, THEN lit, discard ELSE 2drop restore-input throw Ghost gexecute THEN ; @@ -2570,7 +2587,7 @@ Cond: MAXI (THeader (:) ; : :noname ( -- colon-sys ) - X cfalign there + switchrom X cfalign there \ define a nameless ghost here ghostheader dup last-header-ghost ! dup to lastghost (:) ; @@ -2617,7 +2634,7 @@ Cond: [ ( -- ) interpreting-state ;Cond Defer instant-interpret-does>-hook ' noop IS instant-interpret-does>-hook -T has? peephole H [IF] +T has? primcentric H [IF] : does-resolved ( ghost -- ) compile does-exec g>xt T a, H ; [ELSE] @@ -2632,7 +2649,8 @@ T has? peephole H [IF] >TARGET Cond: DOES> - T here 5 cells H + alit, compile (does>2) compile ;s + T here H [ T has? primcentric H [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] T cells + H + alit, compile !does compile ;s doeshandler, resolve-does>-part ;Cond @@ -2822,7 +2840,7 @@ by Create : u, ( n -- udp ) current-region >r user-region activate - X here swap X , tup@ - + X here swap X , tup@ - r> activate ; : au, ( n -- udp ) @@ -2863,9 +2881,22 @@ by User [THEN] +T has? rom H [IF] Builder (Value) Build: ( n -- ) ;Build -by: :docon ( target-body-addr -- n ) T @ H ;DO +by: :dovalue ( target-body-addr -- n ) T @ @ H ;DO + +Builder Value +Build: T here 0 A, H switchram T align here swap ! , H ;Build +by (Value) + +Builder AValue +Build: T here 0 A, H switchram T align here swap ! A, H ;Build +by (Value) +[ELSE] +Builder (Value) +Build: ( n -- ) ;Build +by: :dovalue ( target-body-addr -- n ) T @ H ;DO Builder Value BuildSmart: T , H ;Build @@ -2874,12 +2905,18 @@ by (Value) Builder AValue BuildSmart: T A, H ;Build by (Value) +[THEN] Defer texecute Builder Defer -BuildSmart: ( -- ) [T'] noop T A, H ;Build -by: :dodefer ( ghost -- ) X @ texecute ;DO +T has? rom H [IF] + Build: ( -- ) T here 0 A, H switchram T align here swap ! H [T'] noop T A, H ( switchrom ) ;Build + by: :dodefer ( ghost -- ) X @ X @ texecute ;DO +[ELSE] + BuildSmart: ( -- ) [T'] noop T A, H ;Build + by: :dodefer ( ghost -- ) X @ texecute ;DO +[THEN] Builder interpret/compile: Build: ( inter comp -- ) swap T A, A, H ;Build-immediate @@ -2910,6 +2947,15 @@ by (Field) T 1 cells H dup ; >CROSS +\ ABI-CODE support +Builder (ABI-CODE) +Build: ;Build +by: :doabicode noop ;DO + +BUILDER (;abi-code) +Build: ;Build +by: :do;abicode noop ;DO + \ Input-Methods 01py Builder input-method @@ -2920,6 +2966,25 @@ Builder input-var Build: ( m v size -- m v' ) over T , H + ;Build DO: abort" Not in cross mode" ;DO +\ Mini-OOF + +Builder method +Build: ( m v -- m' v ) over T , swap cell+ swap H ;Build +DO: abort" Not in cross mode" ;DO + +Builder var +Build: ( m v size -- m v+size ) over T , H + ;Build +DO: ( o -- addr ) T @ H + ;DO + +Builder end-class +Build: ( addr m v -- ) + T here >r , dup , 2 cells H ?DO T ['] noop , 1 cells H +LOOP + T cell+ dup cell+ r> rot @ 2 cells /string move H ;Build +by Create + +: class ( class -- class methods vars ) dup T 2@ H ; +: defines ( xt class -- ) T ' >body @ + ! H ; + \ Peephole optimization 05sep01jaw \ this section defines different compilation @@ -2932,7 +2997,7 @@ DO: abort" Not in cross mode" ;DO \ optimizer for cross -T has? peephole H [IF] +T has? primcentric H [IF] \ .( loading peephole optimization) cr @@ -2942,10 +3007,17 @@ T has? peephole H [IF] : (callcm) T here 0 a, 0 a, H ; ' (callcm) plugin-of colonmark, : (call-res) >tempdp resolved gexecute tempdp> drop ; ' (call-res) plugin-of colon-resolve +T has? ec H [IF] +: (pprim) T @ H >signed dup 0< IF $4000 - ELSE + cr ." wrong usage of (prim) " + dup gdiscover IF .ghost ELSE . THEN cr -1 throw THEN + T a, H ; ' (pprim) plugin-of prim, +[ELSE] : (pprim) dup 0< IF $4000 - ELSE cr ." wrong usage of (prim) " dup gdiscover IF .ghost ELSE . THEN cr -1 throw THEN T a, H ; ' (pprim) plugin-of prim, +[THEN] \ if we want this, we have to spilt aconstant \ and constant!! @@ -2969,7 +3041,7 @@ Builder Defer compile: g>body compile lit-perform T A, H ;compile Builder (Field) -compile: g>body T @ H compile lit+ T , H ;compile +compile: g>body T @ H compile lit+ T here H reloff T , H ;compile Builder interpret/compile: compile: does-resolved ;compile @@ -2996,7 +3068,7 @@ compile: does-resolved ;compile \ : ?struc ( flag -- ) ABORT" CROSS: unstructured " ; \ : sys? ( sys -- sys ) dup 0= ?struc ; -: >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ; +: >mark ( -- sys ) T here 0 , H ; X has? abranch [IF] : branchoffset ( src dest -- ) drop ; @@ -3181,6 +3253,10 @@ Cond: ENDCASE endcase, ;Cond 1to compile (+loop) loop] compile unloop skiploop] ; ' (+loop,) plugin-of +loop, +: (-loop,) ( target-addr -- ) + 1to compile (-loop) loop] + compile unloop skiploop] ; ' (-loop,) plugin-of -loop, + : (next,) compile (next) loop] compile unloop ; ' (next,) plugin-of next, @@ -3190,6 +3266,7 @@ Cond: FOR for, ;Cond Cond: LOOP 1 ncontrols? loop, ;Cond Cond: +LOOP 1 ncontrols? +loop, ;Cond +Cond: -LOOP 1 ncontrols? -loop, ;Cond Cond: NEXT 1 ncontrols? next, ;Cond \ String words 23feb93py @@ -3212,10 +3289,19 @@ Cond: ABORT" if, ahead, there [char] >r then, r> compile ALiteral compile c(abort") then, ;Cond [THEN] +X has? rom [IF] +Cond: IS T ' >body @ H compile ALiteral compile ! ;Cond +: IS T >address ' >body @ ! H ; +Cond: TO T ' >body @ H compile ALiteral compile ! ;Cond +: TO T ' >body @ ! H ; +Cond: CTO T ' >body H compile ALiteral compile ! ;Cond +: CTO T ' >body ! H ; +[ELSE] Cond: IS T ' >body H compile ALiteral compile ! ;Cond : IS T >address ' >body ! H ; Cond: TO T ' >body H compile ALiteral compile ! ;Cond : TO T ' >body ! H ; +[THEN] Cond: defers T ' >body @ compile, H ;Cond @@ -3255,7 +3341,7 @@ Cond: postpone ( -- ) \ name hex >CROSS -Create magic s" Gforth3x" here over allot swap move +Create magic s" Gforth4x" here over allot swap move bigendian 1+ \ strangely, in magic big=0, little=1 tcell 1 = 0 and or @@ -3269,6 +3355,7 @@ tchar 8 = 78 and or magic 7 + c! : save-cross ( "image-name" "binary-name" -- ) + .regions \ s" ec" X $has? IF .regions THEN bl parse ." Saving to " 2dup type cr w/o bin create-file throw >r s" header" X $has? IF @@ -3285,7 +3372,8 @@ magic 7 + c! ELSE bl parse 2drop THEN - dictionary >rmem @ there + >rom dictionary >rmem @ there + s" rom" X $has? IF dictionary >rstart @ - THEN r@ write-file throw \ write image s" relocate" X $has? IF dictionary >rbm @ there 1- tcell>bit rshift 1+ @@ -3693,6 +3781,8 @@ previous : 2/ 2/ ; : hex. base @ $10 base ! swap . base ! ; : invert invert ; +: linkstring ( addr u n addr -- ) + X here over X @ X , swap X ! X , ht-string, X align ; \ : . . ; : all-words ['] forced? IS skip? ;