--- gforth/cross.fs 2003/01/03 17:28:12 1.134 +++ gforth/cross.fs 2004/12/01 10:02:59 1.147 @@ -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. @@ -447,7 +447,7 @@ sourcepath value fpath \G Make a complete new Forth search path; the path separator is |. fpath path= ; -: path>counted cell+ dup cell+ swap @ ; +: path>string cell+ dup cell+ swap @ ; : next-path ( adr len -- adr2 len2 ) 2dup 0 scan @@ -456,12 +456,12 @@ sourcepath value fpath r> - ; : previous-path ( path^ -- ) - dup path>counted + dup path>string BEGIN tuck dup WHILE repeat ; : .path ( path-addr -- ) \ gforth \G Display the contents of the search path @var{path-addr}. - path>counted + path>string BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ; : .fpath ( -- ) \ gforth @@ -546,7 +546,7 @@ Create tfile 0 c, 255 chars allot IF rdrop ofile place open-ofile dup 0= IF >r ofile count r> THEN EXIT - ELSE r> path>counted + ELSE r> path>string BEGIN next-path dup WHILE 5 pick 5 pick check-path 0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN @@ -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,20 +1427,8 @@ 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 @@ -1543,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> @@ -1625,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 @@ -1701,7 +1722,8 @@ Ghost (do) Ghost (?do) Ghost (for) drop Ghost (loop) Ghost (+loop) 2drop Ghost (next) drop -Ghost (does>) Ghost (compile) 2drop +Ghost (does>) Ghost (does>1) Ghost (does>2) 2drop drop +Ghost compile, drop Ghost (.") Ghost (S") Ghost (ABORT") 2drop drop Ghost (C") Ghost c(abort") Ghost type 2drop drop Ghost ' drop @@ -2005,7 +2027,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 @@ -2034,7 +2056,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 @@ -2071,8 +2097,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, @@ -2152,8 +2178,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 ; @@ -2178,7 +2205,7 @@ NoHeaderFlag off ENDCASE LOOP ; -Defer setup-execution-semantics +Defer setup-execution-semantics ' noop IS setup-execution-semantics 0 Value lastghost : (THeader ( "name" -- ghost ) @@ -2270,6 +2297,7 @@ 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 +2466,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] @@ -2578,7 +2615,7 @@ Cond: [ ( -- ) interpreting-state ;Cond r@ created >do:ghost ! r@ swap resolve r> tlastcfa @ >tempdp dodoes, tempdp> ; -Defer instant-interpret-does>-hook +Defer instant-interpret-does>-hook ' noop IS instant-interpret-does>-hook T has? peephole H [IF] : does-resolved ( ghost -- ) @@ -2595,8 +2632,8 @@ T has? peephole H [IF] >TARGET Cond: DOES> - compile (does>) doeshandler, - resolve-does>-part + T here 5 cells H + alit, compile (does>2) compile ;s + doeshandler, resolve-does>-part ;Cond : DOES> @@ -2778,20 +2815,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 @@ -2801,6 +2861,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 @@ -2872,6 +2934,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, @@ -3184,7 +3248,7 @@ 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 @@ -3221,10 +3285,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 ; @@ -3616,15 +3680,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? ; @@ -3640,6 +3708,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 ;