--- gforth/cross.fs 2002/12/14 17:34:12 1.130 +++ gforth/cross.fs 2003/11/09 20:47:49 1.143 @@ -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. @@ -662,6 +662,7 @@ stack-warn [IF] : defempty? empty? ; [ELSE] : defempty? ; immediate +\ : defempty? .sourcepos ; [THEN] \ \ -------------------- Compiler Plug Ins 01aug97jaw @@ -1175,14 +1176,21 @@ false DefaultValue backtrace false DefaultValue new-input false DefaultValue peephole false DefaultValue abranch -true DefaultValue control-rack +true DefaultValue f83headerstring +true DefaultValue control-rack [THEN] +true DefaultValue gforthcross true DefaultValue interpreter true DefaultValue ITC false DefaultValue rom true DefaultValue standardthreading +\ ANSForth environment stuff +8 DefaultValue ADDRESS-UNIT-BITS +255 DefaultValue MAX-CHAR +255 DefaultValue /COUNTED-STRING + >TARGET s" relocate" T environment? H \ JAW why set NIL to this?! @@ -1237,11 +1245,9 @@ tbits/char bits/byte / Constant tbyte \ Variables 06oct92py -Variable image Variable (tlast) (tlast) Value tlast TNIL tlast ! \ Last name field Variable tlastcfa \ Last code field -Variable bit$ \ statistics 10jun97jaw @@ -1263,7 +1269,8 @@ Variable region-link \ linked Variable mirrored-link \ linked list for mirrored regions 0 dup mirrored-link ! region-link ! -: >rname 8 cells + ; +: >rname 9 cells + ; +: >rtouch 8 cells + ; \ executed when region is accessed : >rbm 4 cells + ; \ bitfield per cell witch indicates relocation : >rmem 5 cells + ; : >rtype 6 cells + ; \ field per cell witch points to a type struct @@ -1278,6 +1285,8 @@ Variable mirrored-link \ linked >r r@ last-defined-region ! r@ >rlen ! dup r@ >rstart ! r> >rdp ! ; +: uninitialized -1 ABORT" CROSS: Region is uninitialized" ; + : region ( addr len -- "name" ) \G create a new region \ check whether predefined region exists @@ -1287,7 +1296,9 @@ Variable mirrored-link \ linked save-input create restore-input throw here last-defined-region ! over ( startaddr ) , ( length ) , ( dp ) , - region-link linked 0 , 0 , 0 , 0 , bl word count string, + region-link linked 0 , 0 , 0 , 0 , + ['] uninitialized , + bl word count string, ELSE \ store new parameters in region bl word drop >body (region) @@ -1305,6 +1316,9 @@ Variable mirrored-link \ linked \G returns the total area dup >rstart @ swap >rlen @ ; +: dp@ ( region -- dp ) + >rdp @ ; + : mirrored ( -- ) \G mark last defined region as mirrored mirrored-link @@ -1350,6 +1364,10 @@ Variable mirrored-link \ linked 0 0 region address-space \ total memory addressed and used by the target system +0 0 region user-region +\ data for user variables goes here +\ this has to be defined before dictionary or ram-dictionary + 0 0 region dictionary \ rom area for the compiler @@ -1369,6 +1387,21 @@ T has? rom H ' dictionary ALIAS rom-dictionary +: setup-region ( region -- ) + >r + \ allocate mem + r@ >rlen @ allocatetarget + r@ >rmem ! + + r@ >rlen @ + target>bitmask-size allocatetarget + r@ >rbm ! + + r@ >rlen @ + tcell / 1+ cells allocatetarget r@ >rtype ! + + ['] noop r@ >rtouch ! + rdrop ; : setup-target ( -- ) \G initialize target's memory space s" rom" T $has? H @@ -1394,29 +1427,16 @@ T has? rom H WHILE dup 0 >rlink - >r r@ >rlen @ - IF \ allocate mem - r@ >rlen @ allocatetarget dup image ! - r@ >rmem ! - - r@ >rlen @ - target>bitmask-size allocatetarget - dup bit$ ! - r@ >rbm ! - - r@ >rlen @ - tcell / 1+ cells allocatetarget r@ >rtype ! - - rdrop - ELSE r> drop THEN + IF r@ setup-region + THEN rdrop REPEAT drop ; \ MakeKernel 22feb99jaw -: makekernel ( targetsize -- ) +: makekernel ( start targetsize -- ) \G convenience word to setup the memory of the target \G used by main.fs of the c-engine based systems - 100 swap dictionary (region) - setup-target ; + dictionary (region) setup-target ; >MINIMAL : makekernel makekernel ; @@ -1544,7 +1564,9 @@ bigendian 0 >rlink - >r r@ >rlen @ IF dup r@ borders within - IF r> r> drop nip EXIT THEN + IF r> r> drop nip + dup >rtouch @ EXECUTE EXIT + THEN THEN r> drop r> @@ -1626,8 +1648,6 @@ CREATE Bittable 80 c, 40 c, 20 c, 10 c, [ [THEN] ] (>regionbm) swap cell/ -bit ; -: (>image) ( taddr -- absaddr ) image @ + ; - DEFER >image DEFER >ramimage DEFER relon @@ -1702,7 +1722,7 @@ Ghost (do) Ghost (?do) Ghost (for) drop Ghost (loop) Ghost (+loop) 2drop Ghost (next) drop -Ghost (does>) Ghost (compile) 2drop +Ghost (does>) Ghost compile, 2drop Ghost (.") Ghost (S") Ghost (ABORT") 2drop drop Ghost (C") Ghost c(abort") Ghost type 2drop drop Ghost ' drop @@ -2006,7 +2026,7 @@ variable ResolveFlag \ Header states 12dec92py \ : flag! ( 8b -- ) tlast @ dup >r T c@ xor r> c! H ; -bigendian [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+ +X has? f83headerstring bigendian or [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+ : flag! ( w -- ) tlast @ flag+ + dup >r T c@ xor r> c! H ; VARIABLE ^imm @@ -2035,7 +2055,11 @@ $20 constant restrict-mask dup T , H bounds ?DO I c@ T c, H LOOP ; >TARGET +X has? f83headerstring [IF] +: name, ( "name" -- ) bl word count ht-string, X cfalign ; +[ELSE] : name, ( "name" -- ) bl word count ht-lstring, X cfalign ; +[THEN] : view, ( -- ) ( dummy ) ; >CROSS @@ -2072,8 +2096,8 @@ s" kernel.TAGS" r/w create-file throw va s" kernel.tags" r/w create-file throw value vi-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-beg 1 c, 7F c, +Create tag-end 1 c, 01 c, Create tag-bof 1 c, 0C c, Create tag-tab 1 c, 09 c, @@ -2153,8 +2177,9 @@ Defer skip? ' false IS skip? 0= ELSE drop true THEN ; -: doer? ( -- flag ) \ name - Ghost >magic @ = ; +: doer? ( "name" -- 0 | addr ) \ name + Ghost dup >magic @ = + IF >link @ ELSE drop 0 THEN ; : skip-defs ( -- ) BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ; @@ -2270,6 +2295,8 @@ Defer setup-prim-semantics Variable prim# : first-primitive ( n -- ) prim# ! ; +: group 0 word drop prim# @ 1- -$200 and prim# ! ; +: groupadd ( n -- ) drop ; : Primitive ( -- ) \ name >in @ skip? IF drop EXIT THEN >in ! s" prims" T $has? H 0= @@ -2438,6 +2465,15 @@ Cond: ALiteral ( n -- ) alit, ;Cond : Char ( "" -- ) bl word char+ c@ ; Cond: [Char] ( "" -- ) Char lit, ;Cond +: (x#) ( adr len base -- ) + base @ >r base ! 0 0 name >number 2drop drop r> base ! ; + +: d# $0a (x#) ; +: h# $010 (x#) ; + +Cond: d# $0a (x#) lit, ;Cond +Cond: h# $010 (x#) lit, ;Cond + tchar 1 = [IF] Cond: chars ;Cond [THEN] @@ -2580,8 +2616,13 @@ Cond: [ ( -- ) interpreting-state ;Cond Defer instant-interpret-does>-hook +T has? peephole H [IF] : does-resolved ( ghost -- ) compile does-exec g>xt T a, H ; +[ELSE] +: does-resolved ( ghost -- ) + g>xt T a, H ; +[THEN] : resolve-does>-part ( -- ) \ resolve words made by builders @@ -2773,20 +2814,43 @@ by Create \ User variables 04may94py -Variable tup 0 tup ! -Variable tudp 0 tudp ! +: tup@ user-region >rstart @ ; + +\ Variable tup 0 tup ! +\ Variable tudp 0 tudp ! : u, ( n -- udp ) - tup @ tudp @ + T ! H - tudp @ dup T cell+ H tudp ! ; + current-region >r user-region activate + X here swap X , tup@ - + r> activate ; : au, ( n -- udp ) - tup @ tudp @ + T A! H - tudp @ dup T cell+ H tudp ! ; + current-region >r user-region activate + X here swap X a, tup@ - + r> activate ; + +T has? no-userspace H [IF] + +: buildby + ghost >exec @ built >exec ! ; + +Builder User +buildby Variable +by Variable + +Builder 2User +buildby 2Variable +by 2Variable + +Builder AUser +buildby AVariable +by AVariable + +[ELSE] Builder User Build: 0 u, X , ;Build -by: :douser ( ghost -- up-addr ) X @ tup @ + ;DO +by: :douser ( ghost -- up-addr ) X @ tup@ + ;DO Builder 2User Build: 0 u, X , 0 u, drop ;Build @@ -2796,6 +2860,8 @@ Builder AUser Build: 0 au, X , ;Build by User +[THEN] + Builder (Value) Build: ( n -- ) ;Build by: :docon ( target-body-addr -- n ) T @ H ;DO @@ -2867,6 +2933,8 @@ DO: abort" Not in cross mode" ;DO T has? peephole H [IF] +\ .( loading peephole optimization) cr + >CROSS : (callc) compile call T >body a, H ; ' (callc) plugin-of colon, @@ -2929,17 +2997,23 @@ compile: does-resolved ;compile : >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ; -: branchoffset ( src dest -- ) - tchar / ; \ ?? jaw +X has? abranch [IF] + : branchoffset ( src dest -- ) drop ; + : offset, ( n -- ) X A, ; +[ELSE] + : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw + : offset, ( n -- ) X , ; +[THEN] -:noname compile branch X here branchoffset X , ; +:noname compile branch X here branchoffset offset, ; IS branch, ( target-addr -- ) -:noname compile ?branch X here branchoffset X , ; +:noname compile ?branch X here branchoffset offset, ; IS ?branch, ( target-addr -- ) -:noname compile branch T here 0 , H ; +:noname compile branch T here 0 H offset, ; IS branchmark, ( -- branchtoken ) -:noname compile ?branch T here 0 , H ; +:noname compile ?branch T here 0 H offset, ; IS ?branchmark, ( -- branchtoken ) -:noname T here 0 , H ; +:noname T here 0 H offset, ; IS ?domark, ( -- branchtoken ) :noname dup X @ ?struc X here over branchoffset swap X ! ; IS branchtoresolve, ( branchtoken -- ) @@ -3012,7 +3086,7 @@ Cond: ?LEAVE ?leave, ;Cond : loop] ( target-addr -- ) branchto, - dup X here branchoffset X , + dup X here branchoffset offset, tcell - (done) ; : skiploop] ?dup IF branchto, branchtoresolve, THEN ; @@ -3117,55 +3191,6 @@ Cond: LOOP 1 ncontrols? loop, ;Cond Cond: +LOOP 1 ncontrols? +loop, ;Cond Cond: NEXT 1 ncontrols? next, ;Cond -\ Absoulte branches 26sep02jaw - -\ This section defined different semantics for -\ conditionals, using and compiling absolute branches - -X has? abranch [IF] - -Ghost abranch drop -Ghost a?branch drop -Ghost a(?do) drop -Ghost a(do) drop -Ghost a(next) drop -Ghost a(+loop) drop -Ghost a(loop) drop - -:noname compile abranch X a, ; plugin-of branch, - -:noname compile a?branch X a, ; plugin-of ?branch, - -:noname compile abranch T here 0 a, H ; plugin-of branchmark, - -:noname compile a?branch T here 0 a, H ; plugin-of ?branchmark, - -:noname - dup X @ ABORT" CROSS: branch already resolved" - X here swap X a! ; plugin-of branchtoresolve, - -:noname - 0 compile a(?do) ?domark, (leave) - branchtomark, 2 to1 ; plugin-of ?do, - -: aloop] ( target-addr -- ) - branchto, - dup X a, - tcell - (done) ; - -:noname - 1to compile a(loop) aloop] - compile unloop skiploop] ; plugin-of loop, - -:noname - 1to compile a(+loop) aloop] - compile unloop skiploop] ; plugin-of +loop, - -:noname - compile a(next) aloop] compile unloop ; plugin-of next, - -[THEN] - \ String words 23feb93py : ," [char] " parse ht-string, X align ; @@ -3222,14 +3247,14 @@ Cond: postpone ( -- ) \ name ABORT" CROSS: Can't postpone on forward declaration" dup >magic @ = IF (gexecute) - ELSE compile (compile) addr, THEN ;Cond + ELSE >link @ alit, compile compile, THEN ;Cond \ save-cross 17mar93py hex >CROSS -Create magic s" Gforth2x" here over allot swap move +Create magic s" Gforth3x" here over allot swap move bigendian 1+ \ strangely, in magic big=0, little=1 tcell 1 = 0 and or @@ -3259,10 +3284,10 @@ magic 7 + c! ELSE bl parse 2drop THEN - image @ there + dictionary >rmem @ there r@ write-file throw \ write image s" relocate" X $has? IF - bit$ @ there 1- tcell>bit rshift 1+ + dictionary >rbm @ there 1- tcell>bit rshift 1+ r@ write-file throw \ write tags THEN r> close-file throw ; @@ -3654,15 +3679,19 @@ previous : * * ; : / / ; : dup dup ; +: ?dup ?dup ; : over over ; : swap swap ; : rot rot ; : drop drop ; +: 2drop 2drop ; : = = ; : <> <> ; : 0= 0= ; : lshift lshift ; : 2/ 2/ ; +: hex. base @ $10 base ! swap . base ! ; +: invert invert ; \ : . . ; : all-words ['] forced? IS skip? ; @@ -3678,6 +3707,11 @@ previous : require require ; : needs require ; : .( [char] ) parse type ; +: ERROR" [char] " parse + rot + IF cr ." *** " type ." ***" -1 ABORT" CROSS: Target error, see text above" + ELSE 2drop + THEN ; : ." [char] " parse type ; : cr cr ; @@ -3718,7 +3752,6 @@ previous : bye bye ; \ dummy -: group 0 word drop ; \ turnkey direction : H forth ; immediate