aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2023-11-18 08:13:19 -0500
committerClyne Sullivan <clyne@bitgloo.com>2023-11-18 08:13:19 -0500
commit6e4c0430de5ade2c8375ddcdeec93d4df3b163b1 (patch)
tree33886ae29401d4508298c6d63592ccdfb37a0e17
parenta3fca07d87a2d057657af9bb79de9f595e4b1a10 (diff)
add _nx word, use for most of core
not seeing performance improvement yet
-rw-r--r--alee-standalone.cpp3
-rw-r--r--alee.cpp1
-rw-r--r--libalee/corewords.cpp81
-rw-r--r--libalee/corewords.hpp5
-rw-r--r--libalee/dictionary.cpp16
-rw-r--r--libalee/dictionary.hpp2
6 files changed, 74 insertions, 34 deletions
diff --git a/alee-standalone.cpp b/alee-standalone.cpp
index 9f5eae5..95c72e5 100644
--- a/alee-standalone.cpp
+++ b/alee-standalone.cpp
@@ -40,6 +40,9 @@ int main(int argc, char *argv[])
SplitMemDict<sizeof(alee_dat)> dict (alee_dat);
State state (dict, readchar);
+ dict.initialize();
+ CoreWords::initialize(state);
+
std::vector args (argv + 1, argv + argc);
for (const auto& a : args) {
std::ifstream file (a);
diff --git a/alee.cpp b/alee.cpp
index cc9abde..e8194c6 100644
--- a/alee.cpp
+++ b/alee.cpp
@@ -46,6 +46,7 @@ int main(int argc, char *argv[])
#endif // ALEE_MSP430
dict.initialize();
+ CoreWords::initialize(state);
{
std::vector args (argv + 1, argv + argc);
diff --git a/libalee/corewords.cpp b/libalee/corewords.cpp
index 292e3cf..c564cdb 100644
--- a/libalee/corewords.cpp
+++ b/libalee/corewords.cpp
@@ -25,6 +25,48 @@ static DoubleCell popd(State&);
static void pushd(State&, DoubleCell);
LIBALEE_SECTION
+void CoreWords::initialize(State& state)
+{
+ auto& d = state.dict;
+ //d.addNativeWord("_lit", word_lit);
+ d.addNativeWord("drop", word_drop);
+ d.addNativeWord("dup", word_dup);
+ d.addNativeWord("swap", word_swap);
+ d.addNativeWord("pick", word_pick);
+ d.addNativeWord("sys", word_sys);
+ d.addNativeWord("+", word_add);
+ d.addNativeWord("-", word_sub);
+ d.addNativeWord("m*", word_mul);
+ d.addNativeWord("_/", word_div);
+ d.addNativeWord("_%", word_mod);
+ d.addNativeWord("_@", word_peek);
+ d.addNativeWord("_!", word_poke);
+ d.addNativeWord(">r", word_rpush);
+ d.addNativeWord("r>", word_rpop);
+ d.addNativeWord("=", word_eq);
+ d.addNativeWord("<", word_lt);
+ d.addNativeWord("&", word_and);
+ d.addNativeWord("|", word_or);
+ d.addNativeWord("^", word_xor);
+ d.addNativeWord("<<", word_shl);
+ d.addNativeWord(">>", word_shr);
+ d.addNativeWord(":", word_colon);
+ d.addNativeWord("_'", word_tick);
+ //d.addNativeWord("exit", word_exit);
+ //d.addNativeWord(";", word_semic);
+ d.addNativeWord("_jmp0", word_jmp0);
+ d.addNativeWord("_jmp", word_jmp);
+ d.addNativeWord("depth", word_depth);
+ d.addNativeWord("_rdepth", word_rdepth);
+ d.addNativeWord("_in", word_in);
+ d.addNativeWord("_ev", word_ev);
+ d.addNativeWord("find", word_find);
+ d.addNativeWord("_uma", word_uma);
+ d.addNativeWord("u<", word_ult);
+ d.addNativeWord("um/mod", word_ummod);
+}
+
+LIBALEE_SECTION
void CoreWords::run(Cell ins, State& state)
{
Addr index = ins;
@@ -38,45 +80,18 @@ execute:
return;
} else switch (index) {
case token("_lit"): word_lit(state); break;// Execution semantics of `literal`.
- case token("drop"): word_drop(state); break;
- case token("dup"): word_dup(state); break;
- case token("swap"): word_swap(state); break;
- case token("pick"): word_pick(state); break;
- case token("sys"): word_sys(state); break; // Calls user-defined "system" handler.
- case token("+"): word_add(state); break;
- case token("-"): word_sub(state); break;
- case token("m*"): word_mul(state); break; // ( n n -- d )
- case token("_/"): word_div(state); break; // ( d n -- n )
- case token("_%"): word_mod(state); break; // ( d n -- n )
- case token("_@"): word_peek(state); break; // ( addr cell? -- n )
- case token("_!"): word_poke(state); break; // ( n addr cell? -- )
- case token(">r"): word_rpush(state); break;
- case token("r>"): word_rpop(state); break;
- case token("="): word_eq(state); break;
- case token("<"): word_lt(state); break;
- case token("&"): word_and(state); break;
- case token("|"): word_or(state); break;
- case token("^"): word_xor(state); break;
- case token("<<"): word_shl(state); break;
- case token(">>"): word_shr(state); break;
- case token(":"): word_colon(state); break; // Begins definition/compilation of new word.
- case token("_'"): word_tick(state); break; // Collects input word and finds execution token.
case token("execute"):
// TODO reimplement
index = state.pop();
goto execute;
case token("exit"): word_exit(state); break;
case token(";"): word_semic(state); break; // Concludes word definition.
- case token("_jmp0"): word_jmp0(state); break; // Jump if popped value equals zero.
- case token("_jmp"): word_jmp(state); break; // Unconditional jump.
- case token("depth"): word_depth(state); break;
- case token("_rdepth"): word_rdepth(state); break;
- case token("_in"): word_in(state); break; // Fetches more input from the user input source.
- case token("_ev"): word_ev(state); break; // Evaluates words from current input source.
- case token("find"): word_find(state); break;
- case token("_uma"): word_uma(state); break; // ( d u u -- d ): Unsigned multiply-add.
- case token("u<"): word_ult(state); break;
- case token("um/mod"): word_ummod(state); break;
+ case token("_nx"):
+ { auto f = state.beyondip();
+ state.ip() = state.popr();
+ ((void (*)(State&))f)(state);
+ state.verify(state.ip() != 0, Error::exit); }
+ break;
}
ip += sizeof(Cell);
diff --git a/libalee/corewords.hpp b/libalee/corewords.hpp
index 959d2e8..5d2e5ca 100644
--- a/libalee/corewords.hpp
+++ b/libalee/corewords.hpp
@@ -43,6 +43,8 @@ void user_sys(State& state);
class CoreWords
{
public:
+ static void initialize(State& state);
+
/**
* Searches for the token/index of the given word if it is part of the
* fundamental word-set.
@@ -81,7 +83,7 @@ public:
"<<\0>>\0:\0_'\0execute\0"
"exit\0;\0_jmp0\0_jmp\0"
"depth\0_rdepth\0_in\0_ev\0find\0"
- "_uma\0u<\0um/mod\0";
+ "_uma\0u<\0um/mod\0_nx\0";
/**
* Count of total fundamental words.
@@ -114,6 +116,7 @@ private:
return -1;
}
+public:
static void word_lit(State&);
static void word_drop(State&);
static void word_dup(State&);
diff --git a/libalee/dictionary.cpp b/libalee/dictionary.cpp
index e4f6761..e946c59 100644
--- a/libalee/dictionary.cpp
+++ b/libalee/dictionary.cpp
@@ -29,6 +29,22 @@ void Dictionary::initialize()
}
LIBALEE_SECTION
+void Dictionary::addNativeWord(const char *s, void (*func)(State&))
+{
+ const Addr h = read(Here);
+ Addr n = h + sizeof(Cell);
+ for (; *s; ++s, ++n)
+ writebyte(n, *s);
+ addDefinition(Word(h + sizeof(Cell), n));
+ add(CoreWords::token("_nx"));
+ add((Cell)func);
+
+ const auto dcell = h - latest();
+ write(h, (read(h) & 0x1F) | Native | static_cast<Cell>(dcell << DistancePos));
+ latest(h);
+}
+
+LIBALEE_SECTION
Addr Dictionary::allot(Cell amount) noexcept
{
Addr old = here();
diff --git a/libalee/dictionary.hpp b/libalee/dictionary.hpp
index 092026b..cfc56c4 100644
--- a/libalee/dictionary.hpp
+++ b/libalee/dictionary.hpp
@@ -159,6 +159,8 @@ public:
*/
void addDefinition(Word word) noexcept;
+ void addNativeWord(const char *s, void (*func)(State&));
+
/**
* Searches the dictionary for an entry for the given word.
* @param word The dictionary-stored word to search for.