]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
wip: dictionary disassembly llvm
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 2 Jun 2023 13:59:53 +0000 (09:59 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 2 Jun 2023 13:59:53 +0000 (09:59 -0400)
alee.cpp

index c7c9ddf16e0572864c0b2d6388a9bc6d8000871e..bd9c23c2d6f509fdd2a3fb366121541edf8671eb 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
 #include "alee.hpp"
 #include "memdict.hpp"
 
+#include <algorithm>
 #include <charconv>
+#include <cstdio>
 #include <fstream>
 #include <iostream>
 #include <vector>
 
+static void compile(State&);
+
 static bool okay = false;
 
 static void readchar(State&);
@@ -107,6 +111,9 @@ void user_sys(State& state)
     case 4: // load
         load(state);
         break;
+    case 5: // compile
+        compile(state);
+        break;
     default:
         break;
     }
@@ -158,3 +165,37 @@ void parseFile(State& state, std::istream& file)
     }
 }
 
+// Prints all compiled words, their start addresses, and their "disassembly".
+// Hopefully, it won't be too difficult to translate these into LLVM IR.
+void compile(State& state)
+{
+    auto& dict = state.dict;
+
+    Addr latest = dict.latest();
+    Addr attr = 0;
+    do {
+        Addr oldlen = attr >> 6;
+        latest -= oldlen;
+
+        attr = dict.read(latest);
+        auto lw = Word::fromLength(latest + sizeof(Cell), attr & 0x1F);
+
+        if (!(attr & Dictionary::Immediate)) {
+            Addr start = dict.getexec(latest);
+            Addr len = oldlen;
+            len -= start;
+            len += latest;
+
+            std::for_each(lw.begin(&dict), lw.end(&dict), putchar);
+            std::cout << " @ " << start << std::endl;
+
+            for (Addr i = 0; i < len; i += sizeof(Cell)) {
+                Addr addr = start;
+                addr += i;
+                std::cout << '\t' << (Addr)dict.read(addr) << ' ';
+            }
+            std::cout << std::endl;
+        }
+    } while (latest != Dictionary::Begin);
+}
+