]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
streamline single execution
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 26 Feb 2023 00:50:27 +0000 (19:50 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 26 Feb 2023 00:50:27 +0000 (19:50 -0500)
Makefile
corewords.cpp
state.cpp

index 45011b6894d90e32bd0ee639bbded33df0da973a..30e210b6e62f881221f3f13bbf2d3501ca54361d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CXXFLAGS += -std=c++17 -g3 -ggdb -O0 \
             -Wall -Wextra -pedantic -Werror \
-           -fno-exceptions -fno-rtti #-fstack-usage
+            -fno-exceptions -fno-rtti #-fstack-usage
 
 CXXFILES := corewords.cpp dictionary.cpp parser.cpp state.cpp
 OBJFILES := $(subst .cpp,.o,$(CXXFILES))
index 02fcb5364ddb59daf3e60ac0954577db556b2391..6aec5ab2e7979b031ee5a1de7e16682da5ca360e 100644 (file)
 #include <cstring>
 #include <utility>
 
-void CoreWords::run(unsigned int index, State& state)
+Word getword(State& state)
 {
-    auto getword = [&state] {
-        auto word = state.dict.input();
-        while (word.size() == 0) {
-            state.input(state);
-            word = state.dict.input();
-        }
-        return word;
-    };
-    auto newdef = [](Dictionary& dict, Word word) {
-        auto addr = dict.alignhere();
-        dict.addDefinition(word);
-        dict.write(addr,
-            (dict.read(addr) & 0x1F) |
-            ((addr - dict.latest()) << 6));
-        dict.latest(addr);
-    };
-    auto tick = [&state](Word word) {
-        if (auto j = state.dict.find(word); j > 0)
-            state.push(state.dict.getexec(j));
-        else if (auto i = CoreWords::findi(state, word); i >= 0)
-            state.push(i & ~CoreWords::Immediate);
-        else
-            state.push(0);
-    };
+    auto word = state.dict.input();
+    while (word.size() == 0) {
+        state.input(state);
+        word = state.dict.input();
+    }
+    return word;
+}
+void newdef(Dictionary& dict, Word word)
+{
+    auto addr = dict.alignhere();
+    dict.addDefinition(word);
+    dict.write(addr,
+        (dict.read(addr) & 0x1F) |
+        ((addr - dict.latest()) << 6));
+    dict.latest(addr);
+};
+void tick(State& state)
+{
+    auto word = getword(state);
+    if (auto j = state.dict.find(word); j > 0)
+        state.push(state.dict.getexec(j));
+    else if (auto i = CoreWords::findi(state, word); i >= 0)
+        state.push(i & ~CoreWords::Immediate);
+    else
+        state.push(0);
+}
 
+void CoreWords::run(unsigned int index, State& state)
+{
     Cell cell;
     DoubleCell dcell;
 
+execute:
     switch (index) {
     default:
         // must be calling a defined subroutine
@@ -91,13 +96,15 @@ void CoreWords::run(unsigned int index, State& state)
         break;
     case 9: // div ( d n -- n )
         cell = state.pop();
-        dcell = state.pop() << (sizeof(Cell) * 8);
+        dcell = state.pop();
+        dcell <<= sizeof(Cell) * 8;
         dcell |= state.pop();
         state.push(dcell / cell);
         break;
     case 10: // mod ( d n -- n )
         cell = state.pop();
-        dcell = state.pop() << (sizeof(Cell) * 8);
+        dcell = state.pop();
+        dcell <<= sizeof(Cell) * 8;
         dcell |= state.pop();
         state.push(dcell % cell);
         break;
@@ -149,14 +156,15 @@ void CoreWords::run(unsigned int index, State& state)
         state.top() >>= cell;
         break;
     case 22: // colon
-        newdef(state.dict, getword());
+        newdef(state.dict, getword(state));
         state.compiling(true);
         break;
     case 23: // tick
-        tick(getword());
+        tick(state);
         break;
     case 24: // execute
-        run(state.pop(), state);
+        index = state.pop();
+        goto execute;
         break;
     case 25: // exit
         state.ip = state.popr();
index 1510352e000fd86be625bcbb9b76b90505882f4a..56ad8cddd5a293f798f397a053a783a1be980346 100644 (file)
--- a/state.cpp
+++ b/state.cpp
@@ -35,16 +35,17 @@ State::Error State::execute(Addr addr)
 {
     auto stat = setjmp(jmpbuf);
     if (!stat) {
-        if (addr < CoreWords::WordCount) {
-            CoreWords::run(addr, *this);
-        } else {
-            pushr(0);
-            ip = addr - sizeof(Cell);
+        ip = 0;
 
-            do {
-                ip += sizeof(Cell);
-                CoreWords::run(dict.read(ip), *this);
-            } while (ip);
+        auto ins = addr;
+        for (;;) {
+            CoreWords::run(ins, *this);
+
+            if (!ip)
+                break;
+
+            ip += sizeof(Cell);
+            ins = dict.read(ip);
         }
     } else {
         return static_cast<State::Error>(stat);