From 970bbb473c81975d3c7366260c8228fd4fcc8c4d Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 9 Mar 2023 09:16:12 -0500 Subject: [PATCH] linting; reduce msp430 binary size --- Makefile | 4 ++-- alee-msp430.cpp | 6 +++--- alee.cpp | 10 ++++++---- libalee/dictionary.hpp | 2 ++ libalee/state.cpp | 4 ++-- libalee/state.hpp | 39 +++++++++++++++++++++------------------ memdict.hpp | 2 +- splitmemdict.hpp | 15 +++++++++++---- 8 files changed, 48 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 5e75d76..624863d 100644 --- 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)) diff --git a/alee-msp430.cpp b/alee-msp430.cpp index 7c82c03..86582a1 100644 --- a/alee-msp430.cpp +++ b/alee-msp430.cpp @@ -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) {} diff --git a/alee.cpp b/alee.cpp index 1be7337..77c2995 100644 --- 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; diff --git a/libalee/dictionary.hpp b/libalee/dictionary.hpp index 4dcae77..0a2e4d3 100644 --- a/libalee/dictionary.hpp +++ b/libalee/dictionary.hpp @@ -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 diff --git a/libalee/state.cpp b/libalee/state.cpp index ea6c601..b3c98b1 100644 --- a/libalee/state.cpp +++ b/libalee/state.cpp @@ -55,11 +55,11 @@ State::Error State::execute(Addr addr) std::size_t State::size() const noexcept { - return std::distance(dstack, static_cast(dsp)) + 1; + return std::distance(dstack, static_cast(dsp)); } std::size_t State::rsize() const noexcept { - return std::distance(rstack, static_cast(rsp)) + 1; + return std::distance(rstack, static_cast(rsp)); } diff --git a/libalee/state.hpp b/libalee/state.hpp index 28396dc..648b868 100644 --- a/libalee/state.hpp +++ b/libalee/state.hpp @@ -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(Error::push)); - *++dsp = value; + *dsp++ = value; } inline Cell pop() { - if (dsp < dstack) + if (dsp == dstack) std::longjmp(jmpbuf, static_cast(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(Error::pushr)); - *++rsp = value; + *rsp++ = value; } inline Cell popr() { - if (rsp < rstack) + if (rsp == rstack) std::longjmp(jmpbuf, static_cast(Error::popr)); - return *rsp--; + return *--rsp; } inline Cell& top() { - if (dsp < dstack) + if (dsp == dstack) std::longjmp(jmpbuf, static_cast(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(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 diff --git a/memdict.hpp b/memdict.hpp index 398af0f..22841c5 100644 --- a/memdict.hpp +++ b/memdict.hpp @@ -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 { diff --git a/splitmemdict.hpp b/splitmemdict.hpp index 6631947..45a9ee1 100644 --- a/splitmemdict.hpp +++ b/splitmemdict.hpp @@ -21,7 +21,7 @@ #include "alee.hpp" -#include +#include #ifndef MEMDICTSIZE #define MEMDICTSIZE (65536) @@ -32,14 +32,21 @@ template 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& spd): + SplitMemDict(spd.rodict) {} + constexpr auto& operator=(const SplitMemDict& spd) { + *this = SplitMemDict(spd.rodict); + return *this; } virtual Cell read(Addr addr) const noexcept final {