From feeec2952c876ab3519a02f03f6fe134c4ce144e Mon Sep 17 00:00:00 2001 From: zeroflag Date: Sun, 4 Jul 2021 21:09:47 +0200 Subject: [PATCH] temp lookup words are no longer inlined but compiled to a temp scratch area --- src/main/java/com/vectron/fcl/Fcl.java | 1 + src/main/res/raw/locals.forth | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/vectron/fcl/Fcl.java b/src/main/java/com/vectron/fcl/Fcl.java index b4a967c..2623c58 100644 --- a/src/main/java/com/vectron/fcl/Fcl.java +++ b/src/main/java/com/vectron/fcl/Fcl.java @@ -384,6 +384,7 @@ public class Fcl { addPrimitive("false", () -> stack.push(Bool.FALSE)); addPrimitive("nil", () -> stack.push(Nil.INSTANCE)); addPrimitive("here", () -> stack.push(new Num(dp))); + addPrimitive("dp!", () -> dp = stack.pop().intValue() ); addPrimitive("interpret", () -> mode = Mode.INTERPRET); addPrimitive("trace", () -> trace = stack.pop().boolValue()); addPrimitive("lit", () -> stack.push((Obj)heap[ip++])); diff --git a/src/main/res/raw/locals.forth b/src/main/res/raw/locals.forth index 110151c..9ee32d4 100644 --- a/src/main/res/raw/locals.forth +++ b/src/main/res/raw/locals.forth @@ -17,12 +17,17 @@ var: psp ( top of the parameter stack, each allocation ad var: qpsp ( parameter stack pointer used by quotations ) var: frame.allocated ( compile time variable for checking if a frame was already allocated ) var: q.count ( compile time counter for quotations, nested into each other ) +var: ldp +6 val: look-word-size ( compiled size of one lookup word ) 8 val: max#loc ( maximum number of local variables per word ) 1024 val: ps.size ( max pstack size ) ps.size allot val: pstack ( parameter stack for storing the locals ) max#loc allot val: names ( names of the local variables ) +max#loc look-word-size * +allot val: scratch ( scratch area for compiling temporary lookup words ) + 0 q.count ! false frame.allocated ! @@ -68,19 +73,23 @@ false frame.allocated ! frame.allocated @ not if ( is this the first local? ) true frame.allocated ! ['] frame.alloc , ( alloc new stack frame for max#loc ) + scratch ldp ! then (frame.top) ['] lit , #loc @ , ( local index ) ['] - , ['] ! , ( move local to from data stack to the stack frame ) - ['] jmp , (dummy) ( bypass the lookup word ) + here ( save this dp and restore it after the lookup word is compiled to the scratch area ) word dup >names ( store lookup word name ) + ldp @ dp! ( set dp to scratch area ) create postpone: immediate ( compile immediate lookup word ) ['] lit , swap , ( type 1=val 0=var ) ['] lit , #loc @ , ( local index within the frame ) ['] lookup , ['] exit , + here ldp @ - look-word-size != if 'Invalid lookup word size' abort then #loc inc - resolve ; + here ldp ! + dp! ; : -> immediate 1 local ; : => immediate 0 local ;