]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
linting; reduce msp430 binary size
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Mar 2023 14:16:12 +0000 (09:16 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Mar 2023 14:16:12 +0000 (09:16 -0500)
Makefile
alee-msp430.cpp
alee.cpp
libalee/dictionary.hpp
libalee/state.cpp
libalee/state.hpp
memdict.hpp
splitmemdict.hpp

index 5e75d76f2df2b5398c42d93c59c1c49a76bb2363..624863d1090ceb737c65b9a6e5e3eaec10a3642b 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
+            -pedantic -Wall -Wextra -Werror -Weffc++ \
+            -fno-exceptions -fno-threadsafe-statics -fno-rtti #-fstack-usage
 
 CXXFILES := $(wildcard libalee/*.cpp)
 OBJFILES := $(subst .cpp,.o,$(CXXFILES))
index 7c82c033cc2d5b3ba96a920b0f3c3aab9748b3af..86582a17630a1b6343e5a5bb5e3c08925fde8c81 100644 (file)
@@ -90,10 +90,7 @@ int main()
                 *ptr++ = c;
             }
         }
-
     }
-
-    return 0;
 }
 
 static void readchar(State& state)
@@ -157,3 +154,6 @@ void user_sys(State& state)
     }
 }
 
+extern "C" int atexit(void (*)()) { return 0; }
+void operator delete(void *) {}
+void operator delete(void *, std::size_t) {}
index 1be73379ad8d1f20dcf5b61315468d5fe1585fbe..77c2995b0a4b883921d014b7fb44c79ad2c35fa5 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -39,10 +39,12 @@ int main(int argc, char *argv[])
 
     dict.initialize();
 
-    std::vector args (argv + 1, argv + argc);
-    for (const auto& a : args) {
-        std::ifstream file (a);
-        parseFile(parser, state, file);
+    {
+        std::vector args (argv + 1, argv + argc);
+        for (const auto& a : args) {
+            std::ifstream file (a);
+            parseFile(parser, state, file);
+        }
     }
 
     okay = true;
index 4dcae770321b7803a2d4ce26053c2c7f65a26616..0a2e4d305934a2332284b90bc89cb7413461b446 100644 (file)
@@ -73,6 +73,8 @@ public:
 
     bool equal(Word, const char *, unsigned) const noexcept;
     bool equal(Word, Word) const noexcept;
+
+    virtual ~Dictionary() = default;
 };
 
 #endif // ALEEFORTH_DICTIONARY_HPP
index ea6c6018714776a83643502ecd65807d1adbec5d..b3c98b1eb1e75f092d267aa7de0c8892b7043af4 100644 (file)
@@ -55,11 +55,11 @@ State::Error State::execute(Addr addr)
 
 std::size_t State::size() const noexcept
 {
-    return std::distance(dstack, static_cast<const Cell *>(dsp)) + 1;
+    return std::distance(dstack, static_cast<const Cell *>(dsp));
 }
 
 std::size_t State::rsize() const noexcept
 {
-    return std::distance(rstack, static_cast<const Cell *>(rsp)) + 1;
+    return std::distance(rstack, static_cast<const Cell *>(rsp));
 }
 
index 28396dc37e5a3a574c578bebed4b153bc973c987..648b868659bc55dca21a3b710ca4d625913c95f8 100644 (file)
@@ -28,8 +28,9 @@
 constexpr unsigned DataStackSize = 16;
 constexpr unsigned ReturnStackSize = 16;
 
-struct State
+class State
 {
+public:
     enum class Error : int {
         none = 0,
         push,
@@ -45,11 +46,6 @@ struct State
     Dictionary& dict;
     void (*input)(State&);
 
-    Cell dstack[DataStackSize] = {};
-    Cell rstack[ReturnStackSize] = {};
-    Cell *dsp = dstack - 1;
-    Cell *rsp = rstack - 1;
-
     std::jmp_buf jmpbuf = {};
 
     constexpr State(Dictionary& d, void (*i)(State&)):
@@ -64,15 +60,15 @@ struct State
     std::size_t rsize() const noexcept;
 
     inline void push(Cell value) {
-        if (dsp == dstack + DataStackSize - 1)
+        if (dsp == dstack + DataStackSize)
             std::longjmp(jmpbuf, static_cast<int>(Error::push));
-        *++dsp = value;
+        *dsp++ = value;
     }
 
     inline Cell pop() {
-        if (dsp < dstack)
+        if (dsp == dstack)
             std::longjmp(jmpbuf, static_cast<int>(Error::pop));
-        return *dsp--;
+        return *--dsp;
     }
 
     inline Cell beyondip() {
@@ -81,28 +77,35 @@ struct State
     }
 
     inline void pushr(Cell value) {
-        if (rsp == rstack + ReturnStackSize - 1)
+        if (rsp == rstack + ReturnStackSize)
             std::longjmp(jmpbuf, static_cast<int>(Error::pushr));
-        *++rsp = value;
+        *rsp++ = value;
     }
 
     inline Cell popr() {
-        if (rsp < rstack)
+        if (rsp == rstack)
             std::longjmp(jmpbuf, static_cast<int>(Error::popr));
-        return *rsp--;
+        return *--rsp;
     }
 
     inline Cell& top() {
-        if (dsp < dstack)
+        if (dsp == dstack)
             std::longjmp(jmpbuf, static_cast<int>(Error::top));
-        return *dsp;
+        return *(dsp - 1);
     }
 
     inline Cell& pick(std::size_t i) {
-        if (dsp - i < dstack)
+        if (dsp - i == dstack)
             std::longjmp(jmpbuf, static_cast<int>(Error::pick));
-        return *(dsp - i);
+        return *(dsp - i - 1);
     }
+
+private:
+    Cell dstack[DataStackSize] = {};
+    Cell rstack[ReturnStackSize] = {};
+    Cell *dsp = dstack;
+    Cell *rsp = rstack;
+
 };
 
 #endif // ALEEFORTH_STATE_HPP
index 398af0f06b331c0ae6acaa9b1f6e20a4aebdd727..22841c5d50f3fbcbe8ed937587e0bc6372128081 100644 (file)
@@ -28,7 +28,7 @@ constexpr unsigned long int MemDictSize = MEMDICTSIZE;
 
 class MemDict : public Dictionary
 {
-    uint8_t dict[MemDictSize];
+    uint8_t dict[MemDictSize] = {0};
 
 public:
     virtual Cell read(Addr addr) const noexcept final {
index 66319472dcd575d1ff7afed3c5f23c0ee7ad5453..45a9ee1c4df3c02de278ca59ad73b423fbe995c5 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "alee.hpp"
 
-#include <cstring>
+#include <algorithm>
 
 #ifndef MEMDICTSIZE
 #define MEMDICTSIZE (65536)
@@ -32,14 +32,21 @@ template<unsigned long int RON>
 class SplitMemDict : public Dictionary
 {
     const uint8_t *rodict;
-    uint8_t rwdict[MemDictSize];
+    uint8_t rwdict[MemDictSize - Dictionary::Begin] = {0};
     uint8_t extra[Dictionary::Begin];
 
 public:
-    constexpr SplitMemDict(const uint8_t *rod):
+    constexpr explicit SplitMemDict(const uint8_t *rod):
         rodict(rod)
     {
-        std::memcpy(extra, rodict, sizeof(extra));
+        std::copy(rodict, rodict + sizeof(extra), extra);
+    }
+
+    constexpr SplitMemDict(const SplitMemDict<RON>& spd):
+        SplitMemDict(spd.rodict) {}
+    constexpr auto& operator=(const SplitMemDict<RON>& spd) {
+        *this = SplitMemDict(spd.rodict);
+        return *this;
     }
 
     virtual Cell read(Addr addr) const noexcept final {