From 1eefd5f51c228db8d29d64714521d5ea24e43b33 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sun, 26 Nov 2023 18:49:21 -0500 Subject: [PATCH] concise wordwrap --- Makefile | 7 ++-- source/core.cpp | 2 +- source/core.hpp | 2 +- source/state.cpp | 1 + source/state.hpp | 3 +- source/types.hpp | 11 +++--- sprit.cpp | 92 ++++++++++++++++++++++++------------------------ 7 files changed, 59 insertions(+), 59 deletions(-) diff --git a/Makefile b/Makefile index 434f49f..5b7f486 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,10 @@ all: sprit msp430: CXX := msp430-elf-g++ msp430: AR := msp430-elf-ar -msp430: CXXFLAGS += -mmcu=msp430fr2476 -Os #-flto -msp430: LDFLAGS += -L/usr/msp430-elf/usr/include -msp430: sprit +msp430: CXXFLAGS += -mmcu=msp430fr2476 -Os -flto -ffunction-sections -fdata-sections +msp430: CXXFLAGS += -L/usr/msp430-elf/usr/include +msp430: LDFLAGS += -Tmsp430fr2476.ld -Wl,-gc-sections +msp430: source/libsprit.a x86: CXXFLAGS += -m32 x86: sprit diff --git a/source/core.cpp b/source/core.cpp index cc7c267..5842ad8 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -30,7 +30,7 @@ void jump(FuncList ip) // LITERAL's run-time semantics: push the given value onto the stack. static auto literall = WordWrap<[] { push((Cell)*++IP); -}>(); +}>; void compileliteral() { diff --git a/source/core.hpp b/source/core.hpp index 4e4c1e6..8b220e4 100644 --- a/source/core.hpp +++ b/source/core.hpp @@ -33,7 +33,7 @@ constexpr auto fexit = WordWrap<[] { extern FuncList IP; extern Cell rpop(); IP = reinterpret_cast(rpop()); -}>(); +}>; void jump(FuncList ip); /** Jumps to the given instruction pointer. */ void compileliteral(); /** Compiles LITERAL into the current definition. */ diff --git a/source/state.cpp b/source/state.cpp index 5daa772..03ad95d 100644 --- a/source/state.cpp +++ b/source/state.cpp @@ -24,6 +24,7 @@ FuncList IP = nullptr; std::array DICT; +Cell& BASE = DICT[DIdxBase]; Cell& HERE = DICT[DIdxHere]; Cell& LATEST = DICT[DIdxLatest]; Cell& STATE = DICT[DIdxState]; diff --git a/source/state.hpp b/source/state.hpp index 8bdc5dc..b6e5bcf 100644 --- a/source/state.hpp +++ b/source/state.hpp @@ -22,7 +22,7 @@ constexpr Addr DS = 16; /** Data stack size */ constexpr Addr RS = 16; /** Return stack size */ -constexpr auto DictSize = 4096u; /** Dictionary size */ +constexpr auto DictSize = 2048u; /** Dictionary size */ constexpr Addr DIdxBase = 0; constexpr Addr DIdxHere = 1; @@ -39,6 +39,7 @@ constexpr Addr DIdxBegin = DIdxInBuf + 80 * sizeof(char); */ extern std::array DICT; +extern Cell& BASE; extern Cell& HERE; /** Linked to HERE's storage in DICT. */ extern Cell& LATEST; /** Linked to LATEST's storage in DICT. */ extern Cell& STATE; /** Linked to STATE's storage in DICT. */ diff --git a/source/types.hpp b/source/types.hpp index fd0b81e..9627a47 100644 --- a/source/types.hpp +++ b/source/types.hpp @@ -100,13 +100,10 @@ struct WordSet * and used for a word's definition. */ template -auto WordWrap = [] { - constexpr static Func list[1] = { - +[] { (funcs(), ...); } - }; - - return list; -}; +constexpr auto WordWrap = []() noexcept { + constexpr static auto wrapper = +[] { (funcs(), ...); }; + return &wrapper; +}(); enum class Error : int { none = 1, diff --git a/sprit.cpp b/sprit.cpp index 9795520..53f850b 100644 --- a/sprit.cpp +++ b/sprit.cpp @@ -33,59 +33,59 @@ static void discard() { auto v = pop(); (void)v; } static void tobool() { if (*sp()) *sp() = -1; } constinit WordSet words ( - Word("[", WordWrap<[] { STATE = 0; }>()).markImmediate(), - Word("]", WordWrap<[] { STATE = -1; }>()), - Word("@", WordWrap()), - Word("c@", WordWrap()), - Word("!", WordWrap<[] { auto a = (Cell *)pop(); *a = pop(); }>()), - Word("c!", WordWrap<[] { auto a = (char *)pop(); *a = pop(); }>()), - Word("_d", WordWrap<[] { *sp() += (Cell)DICT.data(); }>()), - Word("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>()), + Word("[", WordWrap<[] { STATE = 0; }>).markImmediate(), + Word("]", WordWrap<[] { STATE = -1; }>), + Word("@", WordWrap), + Word("c@", WordWrap), + Word("!", WordWrap<[] { auto a = (Cell *)pop(); *a = pop(); }>), + Word("c!", WordWrap<[] { auto a = (char *)pop(); *a = pop(); }>), + Word("_d", WordWrap<[] { *sp() += (Cell)DICT.data(); }>), + Word("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>), Word("_jmp0", WordWrap<[] { ++IP; if (pop() == 0) jump((FuncList)*IP); - }>()), - Word(",", WordWrap()), - Word("emit", WordWrap<[] { std::putchar(pop()); }>()), - Word("key", WordWrap<[] { push(key()); }>()), - Word("key?", WordWrap<[] { push(haskey()); }, tobool>()), - Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>()), - Word(":", WordWrap()), - Word(";", WordWrap()).markImmediate(), + }>), + Word(",", WordWrap), + Word("emit", WordWrap<[] { std::putchar(pop()); }>), + Word("key", WordWrap<[] { push(key()); }>), + Word("key?", WordWrap<[] { push(haskey()); }, tobool>), + Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>), + Word(":", WordWrap), + Word(";", WordWrap).markImmediate(), Word("exit", fexit), - Word("drop", WordWrap()), - Word("dup", WordWrap<[] { push(*sp()); }>()), - Word("swap", WordWrap<[] { std::swap(*sp(), *(sp() - 1)); }>()), - Word("pick", WordWrap<[] { auto t = *(sp() - *sp() - 1); *sp() = t; }>()), - Word("cells", WordWrap<[] { *sp() *= sizeof(Cell); }>()), - Word("+", WordWrap<[] { *(sp() - 1) += *sp(); }, discard>()), - Word("-", WordWrap<[] { *(sp() - 1) -= *sp(); }, discard>()), - Word("*", WordWrap<[] { *(sp() - 1) *= *sp(); }, discard>()), - Word("/", WordWrap<[] { *(sp() - 1) /= *sp(); }, discard>()), - Word("mod", WordWrap<[] { *(sp() - 1) %= *sp(); }, discard>()), - Word("=", WordWrap<[] { *(sp() - 1) = *(sp() - 1) == *sp(); }, discard, tobool>()), - Word("<", WordWrap<[] { *(sp() - 1) = *(sp() - 1) < *sp(); }, discard, tobool>()), - Word("or", WordWrap<[] { *(sp() - 1) |= *sp(); }, discard>()), - Word("and", WordWrap<[] { *(sp() - 1) &= *sp(); }, discard>()), - Word("xor", WordWrap<[] { *(sp() - 1) ^= *sp(); }, discard>()), - Word("lshift", WordWrap<[] { *(sp() - 1) <<= *sp(); }, discard>()), - Word("rshift", WordWrap<[] { *(sp() - 1) >>= *sp(); }, discard>()), - Word(">r", WordWrap<[] { rpush(pop()); }>()), - Word("r>", WordWrap<[] { push(rpop()); }>()), - Word("immediate", WordWrap<[] { ((Word *)LATEST)->markImmediate(); }>()), - Word("aligned", WordWrap<[] { *sp() = aligned(*sp()); }>()), - Word("align", WordWrap()), - Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>()).markImmediate(), - Word("\'", WordWrap()), - Word("_i", WordWrap<[] { *sp() = ((Word *)*sp())->immediate(); }, tobool>()), - Word("[']", WordWrap()).markImmediate(), - Word("compile,", WordWrap()), - Word("word", WordWrap()), + Word("drop", WordWrap), + Word("dup", WordWrap<[] { push(*sp()); }>), + Word("swap", WordWrap<[] { std::swap(*sp(), *(sp() - 1)); }>), + Word("pick", WordWrap<[] { auto t = *(sp() - *sp() - 1); *sp() = t; }>), + Word("cells", WordWrap<[] { *sp() *= sizeof(Cell); }>), + Word("+", WordWrap<[] { *(sp() - 1) += *sp(); }, discard>), + Word("-", WordWrap<[] { *(sp() - 1) -= *sp(); }, discard>), + Word("*", WordWrap<[] { *(sp() - 1) *= *sp(); }, discard>), + Word("/", WordWrap<[] { *(sp() - 1) /= *sp(); }, discard>), + Word("mod", WordWrap<[] { *(sp() - 1) %= *sp(); }, discard>), + Word("=", WordWrap<[] { *(sp() - 1) = *(sp() - 1) == *sp(); }, discard, tobool>), + Word("<", WordWrap<[] { *(sp() - 1) = *(sp() - 1) < *sp(); }, discard, tobool>), + Word("or", WordWrap<[] { *(sp() - 1) |= *sp(); }, discard>), + Word("and", WordWrap<[] { *(sp() - 1) &= *sp(); }, discard>), + Word("xor", WordWrap<[] { *(sp() - 1) ^= *sp(); }, discard>), + Word("lshift", WordWrap<[] { *(sp() - 1) <<= *sp(); }, discard>), + Word("rshift", WordWrap<[] { *(sp() - 1) >>= *sp(); }, discard>), + Word(">r", WordWrap<[] { rpush(pop()); }>), + Word("r>", WordWrap<[] { push(rpop()); }>), + Word("immediate", WordWrap<[] { ((Word *)LATEST)->markImmediate(); }>), + Word("aligned", WordWrap<[] { *sp() = aligned(*sp()); }>), + Word("align", WordWrap), + Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>).markImmediate(), + Word("\'", WordWrap), + Word("_i", WordWrap<[] { *sp() = ((Word *)*sp())->immediate(); }, tobool>), + Word("[']", WordWrap).markImmediate(), + Word("compile,", WordWrap), + Word("word", WordWrap), Word("_b", WordWrap<[] { std::putchar('#'); // Gives a good breakpoint spot for gdb - }>()), - Word(".", WordWrap<[] { std::cout << pop() << ' '; }>()) + }>), + Word(".", WordWrap<[] { std::cout << pop() << ' '; }>) ); void getinput()