Skip to content

Commit

Permalink
add LIBALEE_SECTION; minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Nov 13, 2023
1 parent 5162349 commit af51fb5
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 28 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ msp430: AR := msp430-elf-gcc-ar
msp430: CXXFLAGS += -I. -I/usr/msp430-elf/usr/include
msp430: CXXFLAGS += -Os -mmcu=msp430fr2476 -ffunction-sections -fdata-sections
msp430: CXXFLAGS += -flto -fno-asynchronous-unwind-tables -fno-threadsafe-statics -fno-stack-protector
msp430: CXXFLAGS += -DALEE_MSP430_HOST
msp430: LDFLAGS += -L msp430 -T msp430fr2476.ld -Wl,-gc-sections -Wl,--no-warn-rwx-segments
msp430: msp430/alee-msp430

Expand Down
2 changes: 1 addition & 1 deletion alee-standalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "alee.hpp"
#include "libalee/alee.hpp"
#include "splitmemdict.hpp"

#include <array>
Expand Down
2 changes: 1 addition & 1 deletion alee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "alee.hpp"
#include "libalee/alee.hpp"
#include "memdict.hpp"

#include <charconv>
Expand Down
3 changes: 0 additions & 3 deletions alee.hpp

This file was deleted.

7 changes: 7 additions & 0 deletions libalee/alee.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "config.hpp"
#include "corewords.hpp"
#include "ctype.hpp"
#include "dictionary.hpp"
#include "parser.hpp"
#include "state.hpp"
#include "types.hpp"
5 changes: 5 additions & 0 deletions libalee/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifndef ALEE_MSP430_HOST
#define LIBALEE_SECTION
#else
#define LIBALEE_SECTION __attribute__((section(".libalee")))
#endif
6 changes: 4 additions & 2 deletions libalee/corewords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "corewords.hpp"
#include "parser.hpp"
#include "alee.hpp"

#include <utility>

LIBALEE_SECTION
void find(State& state, Word word)
{
Cell tok = 0;
Expand All @@ -37,6 +37,7 @@ void find(State& state, Word word)
state.push(imm);
}

LIBALEE_SECTION
void CoreWords::run(Cell ins, State& state)
{
Cell cell;
Expand Down Expand Up @@ -252,6 +253,7 @@ void CoreWords::run(Cell ins, State& state)
ip += sizeof(Cell);
}

LIBALEE_SECTION
Cell CoreWords::findi(State& state, Word word)
{
return findi(word.begin(&state.dict), word.size());
Expand Down
5 changes: 3 additions & 2 deletions libalee/corewords.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#ifndef ALEEFORTH_COREWORDS_HPP
#define ALEEFORTH_COREWORDS_HPP

#include "ctype.hpp"
#include "config.hpp"
#include "types.hpp"
#include "state.hpp"
#include "dictionary.hpp"

/**
* To be implemented by the user, this function is called when the `sys` word
Expand Down Expand Up @@ -61,6 +61,7 @@ class CoreWords

private:
template<typename Iter>
LIBALEE_SECTION
constexpr static Cell findi(Iter it, std::size_t size)
{
const char *ptr = CoreWords::wordsarr;
Expand Down
4 changes: 4 additions & 0 deletions libalee/ctype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ constexpr inline bool isupper(uint8_t c) {
return c >= 'A' && c <= 'Z';
}

constexpr inline bool islower(uint8_t c) {
return c >= 'a' && c <= 'z';
}

constexpr inline bool isalpha(uint8_t c) {
return isupper(c) || (c >= 'a' && c <= 'z');
}
Expand Down
14 changes: 13 additions & 1 deletion libalee/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "dictionary.hpp"
#include "alee.hpp"

LIBALEE_SECTION
void Dictionary::initialize()
{
write(Base, 10);
Expand All @@ -27,6 +28,7 @@ void Dictionary::initialize()
write(Source, Input + sizeof(Cell));
}

LIBALEE_SECTION
Addr Dictionary::allot(Cell amount) noexcept
{
Addr old = here();
Expand All @@ -41,22 +43,26 @@ Addr Dictionary::allot(Cell amount) noexcept
return old;
}

LIBALEE_SECTION
void Dictionary::add(Cell value) noexcept
{
write(allot(sizeof(Cell)), value);
}

LIBALEE_SECTION
Addr Dictionary::aligned(Addr addr)
{
return (addr + (sizeof(Cell) - 1)) & ~(sizeof(Cell) - 1);
}

LIBALEE_SECTION
Addr Dictionary::alignhere() noexcept
{
here(aligned(here()));
return here();
}

LIBALEE_SECTION
void Dictionary::addDefinition(Word word) noexcept
{
Cell wsize = word.size();
Expand All @@ -75,6 +81,7 @@ void Dictionary::addDefinition(Word word) noexcept
alignhere();
}

LIBALEE_SECTION
Addr Dictionary::find(Word word) noexcept
{
Addr lt = latest();
Expand Down Expand Up @@ -106,6 +113,7 @@ Addr Dictionary::find(Word word) noexcept
return 0;
}

LIBALEE_SECTION
Addr Dictionary::getexec(Addr addr) noexcept
{
const Addr l = read(addr);
Expand All @@ -119,6 +127,7 @@ Addr Dictionary::getexec(Addr addr) noexcept
return aligned(addr);
}

LIBALEE_SECTION
bool Dictionary::hasInput() const noexcept
{
const Addr src = read(Dictionary::Source);
Expand All @@ -140,6 +149,7 @@ bool Dictionary::hasInput() const noexcept
return false;
}

LIBALEE_SECTION
Word Dictionary::input() noexcept
{
const Addr src = read(Dictionary::Source);
Expand Down Expand Up @@ -169,11 +179,13 @@ Word Dictionary::input() noexcept
return Word(wstart, wend);
}

LIBALEE_SECTION
bool Dictionary::equal(Word word, const char *str, unsigned len) const noexcept
{
return word.size() == len && equal(word.begin(this), word.end(this), str);
}

LIBALEE_SECTION
bool Dictionary::equal(Word word, Word other) const noexcept
{
return word.size() == other.size() && equal(word.begin(this), word.end(this), other.begin(this));
Expand Down
10 changes: 8 additions & 2 deletions libalee/dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#ifndef ALEEFORTH_DICTIONARY_HPP
#define ALEEFORTH_DICTIONARY_HPP

#include "ctype.hpp"
#include "config.hpp"
#include "types.hpp"
#include "ctype.hpp"

#include <algorithm>
#include <cstddef>
Expand Down Expand Up @@ -72,10 +73,14 @@ struct Dictionary
*/
void initialize();

LIBALEE_SECTION
Addr here() const noexcept { return read(Here); }
LIBALEE_SECTION
void here(Addr l) noexcept { write(Here, l); }

LIBALEE_SECTION
Addr latest() const noexcept { return read(Latest); }
LIBALEE_SECTION
void latest(Addr l) noexcept { write(Latest, l); }

// Aligns the given address.
Expand Down Expand Up @@ -124,6 +129,7 @@ struct Dictionary

// Used for case-insensitive comparison between two iterators.
template<typename Iter1, typename Iter2>
LIBALEE_SECTION
constexpr static bool equal(Iter1 b1, Iter1 e1, Iter2 b2) {
return std::equal(b1, e1, b2, eqchars);
}
Expand All @@ -132,6 +138,7 @@ struct Dictionary

private:
// Case-insensitive comparison.
LIBALEE_SECTION
constexpr static bool eqchars(char c1, char c2) {
if (isalpha(static_cast<uint8_t>(c1)))
c1 |= 32;
Expand All @@ -140,7 +147,6 @@ struct Dictionary

return c1 == c2;
}

};

#endif // ALEEFORTH_DICTIONARY_HPP
Expand Down
9 changes: 6 additions & 3 deletions libalee/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "corewords.hpp"
#include "ctype.hpp"
#include "parser.hpp"
#include "alee.hpp"

Error (*Parser::customParse)(State&, Word) = nullptr;

LIBALEE_SECTION
Error Parser::parse(State& state, const char *str)
{
auto addr = Dictionary::Input;
Expand All @@ -40,6 +39,7 @@ Error Parser::parse(State& state, const char *str)
return parseSource(state);
}

LIBALEE_SECTION
Error Parser::parseSource(State& state)
{
auto err = Error::none;
Expand All @@ -50,6 +50,7 @@ Error Parser::parseSource(State& state)
return err;
}

LIBALEE_SECTION
Error Parser::parseWord(State& state, Word word)
{
bool imm;
Expand Down Expand Up @@ -81,6 +82,7 @@ Error Parser::parseWord(State& state, Word word)
return Error::none;
}

LIBALEE_SECTION
Error Parser::parseNumber(State& state, Word word)
{
const auto base = state.dict.read(Dictionary::Base);
Expand Down Expand Up @@ -113,6 +115,7 @@ Error Parser::parseNumber(State& state, Word word)
return Error::none;
}

LIBALEE_SECTION
void Parser::processLiteral(State& state, Cell value)
{
if (state.compiling()) {
Expand Down
2 changes: 2 additions & 0 deletions libalee/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#ifndef ALEEFORTH_PARSER_HPP
#define ALEEFORTH_PARSER_HPP

#include "config.hpp"
#include "types.hpp"
#include "state.hpp"

#include <string_view>

Expand Down
11 changes: 9 additions & 2 deletions libalee/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "corewords.hpp"
#include "state.hpp"
#include "alee.hpp"

#include <iterator>

LIBALEE_SECTION
bool State::compiling() const
{
return dict.read(Dictionary::Compiling);
}

LIBALEE_SECTION
void State::compiling(bool yes)
{
dict.write(Dictionary::Compiling, yes);
}

LIBALEE_SECTION
State::Context State::save()
{
return context;
}

LIBALEE_SECTION
void State::load(const State::Context& ctx)
{
context = ctx;
}

LIBALEE_SECTION
Error State::execute(Addr addr)
{
auto stat = static_cast<Error>(setjmp(context.jmpbuf));
Expand All @@ -63,6 +67,7 @@ Error State::execute(Addr addr)
return stat;
}

LIBALEE_SECTION
void State::reset()
{
while (size())
Expand All @@ -74,11 +79,13 @@ void State::reset()
context.ip = 0;
}

LIBALEE_SECTION
std::size_t State::size() const noexcept
{
return dsp - dstack;
}

LIBALEE_SECTION
std::size_t State::rsize() const noexcept
{
return rsp - rstack;
Expand Down
Loading

0 comments on commit af51fb5

Please sign in to comment.