From c8c337fd5860e0431aba4eaffb8531e32e6c95bc Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 10 Apr 2020 07:53:41 -0400 Subject: [PATCH] xcomp: widen scope We can now cross-compile core.fs. --- emul/forth/xcomp.fs | 13 +++++++++++-- forth/xcomp.fs | 54 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/emul/forth/xcomp.fs b/emul/forth/xcomp.fs index 73b3340..84a250c 100644 --- a/emul/forth/xcomp.fs +++ b/emul/forth/xcomp.fs @@ -1,11 +1,20 @@ +: _ + ['] EXIT , + R> DROP ( exit : ) + XCOFF +; IMMEDIATE +';' CURRENT @ 4 - C! + +: (find) (xfind) ; +: ['] X['] ; IMMEDIATE +: COMPILE XCOMPILE ; IMMEDIATE : CODE XCODE ; : IMMEDIATE XIMM ; : : [ ' X: , ] ; CURRENT @ XCURRENT ! -H@ ' _bend - 4 + XOFF ! ( dummy entry for dict hook ) (xentry) _ H@ 256 /MOD 2 PC! 2 PC! - +H@ ' _bend - XOFF ! diff --git a/forth/xcomp.fs b/forth/xcomp.fs index 513b11b..236b331 100644 --- a/forth/xcomp.fs +++ b/forth/xcomp.fs @@ -14,18 +14,29 @@ "'" and friends will *not* find words you're about to define. Only (xfind) will. - Words ":", "IMMEDIATE" and "CODE" are not automatically - shadowed to allow the harmless inclusion of this unit. This - shadowing has to take place in your xcomp configuration. + Words overrides like ":", "IMMEDIATE" and "CODE" are not + automatically shadowed to allow the harmless inclusion of + this unit. This shadowing has to take place in your xcomp + configuration. See example in /emul/forth/xcomp.fs + + Note that this cross compilation unit is far from foolproof + and cannot cross compile any kind of code. Cross compliation + of Forth dicts is *tricky*. This unit is designed to cross + compile the core full interpreter, that is, the contents + of the "/forth" folder of the project. + + Cross compiling anything else might work, but as soon as + you start defining immediates and using them on-the-fly, + things get icky. ) VARIABLE XCURRENT VARIABLE XOFF : XCON XCURRENT CURRENT* ! ; -: XCOFF CURRENT CURRENT* ! ; +: XCOFF 0x02 RAM+ CURRENT* ! ; : (xentry) XCON (entry) XCOFF ; @@ -33,15 +44,37 @@ VARIABLE XOFF : XIMM XCON IMMEDIATE XCOFF ; +: XAPPLY + DUP XOFF @ > IF XOFF @ - THEN +; + +( Run find in XCURRENT and apply XOFF ) +: (xfind) + XCURRENT @ SWAP ( xcur w ) + _find ( a f ) + IF ( a ) + ( apply XOFF ) + XAPPLY 1 + ELSE + 0 + THEN +; + +: X' XCON ' XCOFF XAPPLY ; +: X['] X' LITN ; +( TODO: am I making the word "," stable here? ) +: XCOMPILE X' LITN ['] , , ; + : X: - XCON - (entry) + (xentry) ( 0e == compiledWord ) [ 0x0e LITN ] , BEGIN ( DUP is because we need a copy in case it's IMMED ) WORD DUP - (find) ( w a f ) + ( cross compile CURRENT ) + XCURRENT @ SWAP ( w xcur w ) + _find ( w a f ) IF ( is word ) DUP IMMED? @@ -49,16 +82,16 @@ VARIABLE XOFF ( When encountering IMMEDIATE, we exec the *host* word. ) DROP ( w ) - ( hardcoded system CURRENT ) + ( system CURRENT ) 0x02 RAM+ @ SWAP ( cur w ) _find ( a f ) NOT IF ABORT THEN ( a ) - EXECUTE + XCON EXECUTE XCOFF ELSE ( not an immed. drop backup w and write, with offset. ) SWAP DROP ( a ) - DUP 0x100 > IF XOFF @ - THEN + XAPPLY , THEN ELSE ( w a ) @@ -67,5 +100,4 @@ VARIABLE XOFF (parse*) @ EXECUTE LITN THEN AGAIN - XCOFF ;