Compare commits

...

8 Commits

@ -40,6 +40,8 @@ int main(int argc, char *argv[])
SplitMemDict<sizeof(alee_dat)> dict (alee_dat); SplitMemDict<sizeof(alee_dat)> dict (alee_dat);
State state (dict, readchar); State state (dict, readchar);
//dict.initialize();
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);
for (const auto& a : args) { for (const auto& a : args) {
std::ifstream file (a); std::ifstream file (a);

@ -99,10 +99,6 @@
: j postpone 2r> ['] r> , postpone r@ ['] swap , : j postpone 2r> ['] r> , postpone r@ ['] swap ,
['] >r , ['] -rot , postpone 2>r ; imm ['] >r , ['] -rot , postpone 2>r ; imm
: aligned dup [ 1 cells 1- ] literal swap over & if [ 1 cells ] literal
swap - + else drop then ;
: align here dup aligned swap - allot ;
: and & ; : and & ;
: or | ; : or | ;
: xor ^ ; : xor ^ ;
@ -114,6 +110,9 @@
: _msb [ 1 1 cells 8 * 1- << ] literal ; : _msb [ 1 1 cells 8 * 1- << ] literal ;
: 2/ dup 1 >> swap 0< if _msb or then ; : 2/ dup 1 >> swap 0< if _msb or then ;
: aligned [ 1 cells 1- ] literal swap over + swap invert and ;
: align here dup aligned swap - allot ;
: /mod 2dup % -rot / ; : /mod 2dup % -rot / ;
: */ >r m* r> _/ ; : */ >r m* r> _/ ;
: sm/rem >r 2dup r@ _% -rot r> _/ ; : sm/rem >r 2dup r@ _% -rot r> _/ ;
@ -221,8 +220,8 @@
dup _isdigit - _uma dup _isdigit - _uma
r> char+ r> 1- repeat ; r> char+ r> 1- repeat ;
: <# 40 here c! ; : <# [ 20 cells ] literal here c! ;
: #> 2drop here dup c@ + 40 here c@ - ; : #> 2drop here dup c@ + [ 20 cells ] literal here c@ - ;
: hold -1 here +! here dup c@ + c! ; : hold -1 here +! here dup c@ + c! ;
: # base @ : # base @
>r 0 i um/mod r> swap >r um/mod r> >r 0 i um/mod r> swap >r um/mod r>

@ -6,8 +6,8 @@
: words _latest @ begin : words _latest @ begin
dup @ dup 31 & dup @ dup 31 &
2 pick cell+ \ lt l len ws 2 pick cell+ \ lt l len ws
2 pick 6 >> 1023 < if \ lt l len ws 2 pick 7 >> 1023 < if \ lt l len ws
rot 6 >> else \ lt len ws adv rot 7 >> else \ lt len ws adv
>r cell+ rot drop r> @ then >r cell+ rot drop r> @ then
-rot swap type space \ lt adv -rot swap type space \ lt adv
over _begin <> while - repeat 2drop ; over _begin <> while - repeat 2drop ;

@ -27,207 +27,15 @@ static void pushd(State&, DoubleCell);
LIBALEE_SECTION LIBALEE_SECTION
void CoreWords::run(Cell ins, State& state) void CoreWords::run(Cell ins, State& state)
{ {
Cell cell;
DoubleCell dcell;
Addr index = ins; Addr index = ins;
auto& ip = state.ip();
execute:
if (index >= Dictionary::Begin) { if (index >= Dictionary::Begin) {
// must be calling a defined subroutine auto& ip = state.ip();
state.pushr(ip); state.pushr(ip);
ip = index; ip = static_cast<Addr>(index - sizeof(Cell));
return; } else {
} else switch (index) { wordstbl[index](state);
case token("_lit"): // Execution semantics of `literal`.
state.push(state.beyondip());
break;
case token("drop"):
state.pop();
break;
case token("dup"):
state.push(state.top());
break;
case token("swap"):
std::swap(state.top(), state.pick(1));
break;
case token("pick"):
state.push(state.pick(state.pop()));
break;
case token("sys"): // Calls user-defined "system" handler.
user_sys(state);
break;
case token("+"):
cell = state.pop();
state.top() += cell;
break;
case token("-"):
cell = state.pop();
state.top() -= cell;
break;
case token("m*"): // ( n n -- d )
cell = state.pop();
dcell = state.pop() * cell;
pushd(state, dcell);
break;
case token("_/"): // ( d n -- n )
cell = state.pop();
dcell = popd(state);
state.push(static_cast<Cell>(dcell / cell));
break;
case token("_%"): // ( d n -- n )
cell = state.pop();
dcell = popd(state);
state.push(static_cast<Cell>(dcell % cell));
break;
case token("_@"): // ( addr cell? -- n )
if (state.pop())
state.push(state.dict.read(state.pop()));
else
state.push(state.dict.readbyte(state.pop()));
break;
case token("_!"): // ( n addr cell? -- )
cell = state.pop();
if (auto addr = state.pop(); cell)
state.dict.write(addr, state.pop());
else
state.dict.writebyte(addr, state.pop() & 0xFFu);
break;
case token(">r"):
state.pushr(state.pop());
break;
case token("r>"):
state.push(state.popr());
break;
case token("="):
cell = state.pop();
state.top() = state.top() == cell ? -1 : 0;
break;
case token("<"):
cell = state.pop();
state.top() = state.top() < cell ? -1 : 0;
break;
case token("&"):
cell = state.pop();
state.top() &= cell;
break;
case token("|"):
cell = state.pop();
state.top() |= cell;
break;
case token("^"):
cell = state.pop();
state.top() ^= cell;
break;
case token("<<"):
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
break;
case token(">>"):
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
break;
case token(":"): // Begins definition/compilation of new word.
state.push(state.dict.alignhere());
state.dict.write(Dictionary::CompToken, state.top());
while (!state.dict.hasInput())
state.input();
state.dict.addDefinition(state.dict.input());
state.compiling(true);
break;
case token("_'"): // Collects input word and finds execution token.
while (!state.dict.hasInput())
state.input();
find(state, state.dict.input());
break;
case token("execute"):
index = state.pop();
goto execute;
case token("exit"):
ip = state.popr();
state.verify(ip != 0, Error::exit);
break;
case token(";"): // Concludes word definition.
state.dict.add(token("exit"));
state.compiling(false);
cell = state.pop();
dcell = cell - state.dict.latest();
if (dcell >= Dictionary::MaxDistance) {
// Large distance to previous entry: store in dedicated cell.
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell),
static_cast<Cell>(dcell));
dcell = Dictionary::MaxDistance;
}
state.dict.write(cell,
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
state.dict.latest(cell);
break;
case token("_jmp0"): // Jump if popped value equals zero.
if (state.pop()) {
state.beyondip();
break;
}
[[fallthrough]];
case token("_jmp"): // Unconditional jump.
ip = state.beyondip();
return;
case token("depth"):
state.push(static_cast<Cell>(state.size()));
break;
case token("_rdepth"):
state.push(static_cast<Cell>(state.rsize()));
break;
case token("_in"): // Fetches more input from the user input source.
state.input();
break;
case token("_ev"): // Evaluates words from current input source.
{
const auto st = state.save();
ip = 0;
Parser::parseSource(state);
state.load(st);
}
break;
case token("find"):
cell = state.pop();
find(state,
Word::fromLength(static_cast<Addr>(cell + 1),
state.dict.readbyte(cell)));
break;
case token("_uma"): // ( d u u -- d ): Unsigned multiply-add.
{
const auto plus = state.pop();
cell = state.pop();
dcell = popd(state);
dcell *= static_cast<Addr>(cell);
dcell += static_cast<Addr>(plus);
pushd(state, dcell);
}
break;
case token("u<"):
cell = state.pop();
state.top() = static_cast<Addr>(state.top()) <
static_cast<Addr>(cell) ? -1 : 0;
break;
case token("um/mod"):
cell = state.pop();
dcell = popd(state);
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) %
static_cast<Addr>(cell)));
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) /
static_cast<Addr>(cell)));
break;
default: // Compacted literals (WordCount <= ins < Begin).
state.push(ins - WordCount);
break;
} }
ip += sizeof(Cell);
} }
LIBALEE_SECTION LIBALEE_SECTION
@ -267,3 +75,183 @@ void pushd(State& s, DoubleCell d)
s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8))); s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
} }
void CoreWords::word_lit(State& state) { // Execution semantics of `literal`.
state.push(state.beyondip());
}
void CoreWords::word_drop(State& state) {
state.pop();
}
void CoreWords::word_dup(State& state) {
state.push(state.top());
}
void CoreWords::word_swap(State& state) {
std::swap(state.top(), state.pick(1));
}
void CoreWords::word_pick(State& state) {
state.push(state.pick(state.pop()));
}
void CoreWords::word_sys(State& state) { // Calls user-defined "system" handler.
user_sys(state);
}
void CoreWords::word_add(State& state) {
auto cell = state.pop();
state.top() += cell;
}
void CoreWords::word_sub(State& state) {
auto cell = state.pop();
state.top() -= cell;
}
void CoreWords::word_mul(State& state) { // ( n n -- d )
auto cell = state.pop();
auto dcell = (DoubleCell)state.pop() * cell;
pushd(state, dcell);
}
void CoreWords::word_div(State& state) { // ( d n -- n )
auto cell = state.pop();
auto dcell = (DoubleCell)popd(state);
state.push(static_cast<Cell>(dcell / cell));
}
void CoreWords::word_mod(State& state) { // ( d n -- n )
auto cell = state.pop();
auto dcell = (DoubleCell)popd(state);
state.push(static_cast<Cell>(dcell % cell));
}
void CoreWords::word_peek(State& state) { // ( addr cell? -- n )
if (state.pop())
state.push(state.dict.read(state.pop()));
else
state.push(state.dict.readbyte(state.pop()));
}
void CoreWords::word_poke(State& state) { // ( n addr cell? -- )
auto cell = state.pop();
if (auto addr = state.pop(); cell)
state.dict.write(addr, state.pop());
else
state.dict.writebyte(addr, state.pop() & 0xFFu);
}
void CoreWords::word_rpush(State& state) {
state.pushr(state.pop());
}
void CoreWords::word_rpop(State& state) {
state.push(state.popr());
}
void CoreWords::word_eq(State& state) {
auto cell = state.pop();
state.top() = state.top() == cell ? -1 : 0;
}
void CoreWords::word_lt(State& state) {
auto cell = state.pop();
state.top() = state.top() < cell ? -1 : 0;
}
void CoreWords::word_and(State& state) {
auto cell = state.pop();
state.top() &= cell;
}
void CoreWords::word_or(State& state) {
auto cell = state.pop();
state.top() |= cell;
}
void CoreWords::word_xor(State& state) {
auto cell = state.pop();
state.top() ^= cell;
}
void CoreWords::word_shl(State& state) {
auto cell = state.pop();
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
}
void CoreWords::word_shr(State& state) {
auto cell = state.pop();
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
}
void CoreWords::word_colon(State& state) { // Begins definition/compilation of new word.
state.push(state.dict.alignhere());
state.dict.write(Dictionary::CompToken, state.top());
while (!state.dict.hasInput())
state.input();
state.dict.addDefinition(state.dict.input());
state.compiling(true);
}
void CoreWords::word_tick(State& state) { // Collects input word and finds execution token.
while (!state.dict.hasInput())
state.input();
find(state, state.dict.input());
}
void CoreWords::word_execute(State& state) {
run(state.pop(), state);
}
void CoreWords::word_exit(State& state) {
state.ip() = state.popr();
state.verify(state.ip() != 0, Error::exit);
}
void CoreWords::word_semic(State& state) { // Concludes word definition.
state.dict.add(token("exit"));
state.compiling(false);
auto cell = state.pop();
auto dcell = (DoubleCell)cell - state.dict.latest();
if (dcell >= Dictionary::MaxDistance) {
// Large distance to previous entry: store in dedicated cell.
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell),
static_cast<Cell>(dcell));
dcell = Dictionary::MaxDistance;
}
state.dict.write(cell,
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << Dictionary::DistancePos));
state.dict.latest(cell);
}
void CoreWords::word_jmp0(State& state) { // Jump if popped value equals zero.
if (state.pop()) {
state.beyondip();
} else {
state.ip() = static_cast<Addr>(state.beyondip() - sizeof(Cell));
}
}
void CoreWords::word_jmp(State& state) { // Unconditional jump.
state.ip() = static_cast<Addr>(state.beyondip() - sizeof(Cell));
}
void CoreWords::word_depth(State& state) {
state.push(static_cast<Cell>(state.size()));
}
void CoreWords::word_rdepth(State& state) {
state.push(static_cast<Cell>(state.rsize()));
}
void CoreWords::word_in(State& state) { // Fetches more input from the user input source.
state.input();
}
void CoreWords::word_ev(State& state) { // Evaluates words from current input source.
const auto st = state.save();
state.ip() = 0;
Parser::parseSource(state);
state.load(st);
}
void CoreWords::word_find(State& state) {
auto cell = state.pop();
find(state,
Word::fromLength(static_cast<Addr>(cell + 1),
state.dict.readbyte(cell)));
}
void CoreWords::word_uma(State& state) { // ( d u u -- d ): Unsigned multiply-add.
const auto plus = state.pop();
auto cell = state.pop();
auto dcell = popd(state);
dcell *= static_cast<Addr>(cell);
dcell += static_cast<Addr>(plus);
pushd(state, dcell);
}
void CoreWords::word_ult(State& state) {
auto cell = state.pop();
state.top() = static_cast<Addr>(state.top()) <
static_cast<Addr>(cell) ? -1 : 0;
}
void CoreWords::word_ummod(State& state) {
auto cell = state.pop();
auto dcell = popd(state);
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) %
static_cast<Addr>(cell)));
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) /
static_cast<Addr>(cell)));
}

@ -81,7 +81,7 @@ public:
"<<\0>>\0:\0_'\0execute\0" "<<\0>>\0:\0_'\0execute\0"
"exit\0;\0_jmp0\0_jmp\0" "exit\0;\0_jmp0\0_jmp\0"
"depth\0_rdepth\0_in\0_ev\0find\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. * Count of total fundamental words.
@ -113,6 +113,85 @@ private:
return -1; return -1;
} }
public:
static void word_lit(State&);
static void word_drop(State&);
static void word_dup(State&);
static void word_swap(State&);
static void word_pick(State&);
static void word_sys(State&);
static void word_add(State&);
static void word_sub(State&);
static void word_mul(State&);
static void word_div(State&);
static void word_mod(State&);
static void word_peek(State&);
static void word_poke(State&);
static void word_rpush(State&);
static void word_rpop(State&);
static void word_eq(State&);
static void word_lt(State&);
static void word_and(State&);
static void word_or(State&);
static void word_xor(State&);
static void word_shl(State&);
static void word_shr(State&);
static void word_colon(State&);
static void word_tick(State&);
static void word_execute(State&);
static void word_exit(State&);
static void word_semic(State&);
static void word_jmp0(State&);
static void word_jmp(State&);
static void word_depth(State&);
static void word_rdepth(State&);
static void word_in(State&);
static void word_ev(State&);
static void word_find(State&);
static void word_uma(State&);
static void word_ult(State&);
static void word_ummod(State&);
constexpr static void (*wordstbl[])(State&) = {
word_lit,
word_drop,
word_dup,
word_swap,
word_pick,
word_sys,
word_add,
word_sub,
word_mul,
word_div,
word_mod,
word_peek,
word_poke,
word_rpush,
word_rpop,
word_eq,
word_lt,
word_and,
word_or,
word_xor,
word_shl,
word_shr,
word_colon,
word_tick,
word_execute,
word_exit,
word_semic,
word_jmp0,
word_jmp,
word_depth,
word_rdepth,
word_in,
word_ev,
word_find,
word_uma,
word_ult,
word_ummod
};
}; };
#endif // ALEEFORTH_COREWORDS_HPP #endif // ALEEFORTH_COREWORDS_HPP

@ -91,14 +91,14 @@ Addr Dictionary::find(Word word) noexcept
const Addr len = l & 0x1F; const Addr len = l & 0x1F;
Word lw; Word lw;
if ((l >> 6) < MaxDistance) { if ((l >> DistancePos) < MaxDistance) {
lw = Word::fromLength(lt + sizeof(Cell), len); lw = Word::fromLength(lt + sizeof(Cell), len);
if (equal(word, lw)) if (equal(word, lw))
return lt; return lt;
else if (lt == Begin) else if (lt == Begin)
break; break;
else else
lt -= l >> 6; lt -= l >> DistancePos;
} else { } else {
lw = Word::fromLength(lt + 2 * sizeof(Cell), len); lw = Word::fromLength(lt + 2 * sizeof(Cell), len);
if (equal(word, lw)) if (equal(word, lw))
@ -120,7 +120,7 @@ Addr Dictionary::getexec(Addr addr) noexcept
const Addr len = l & 0x1Fu; const Addr len = l & 0x1Fu;
addr += sizeof(Cell); addr += sizeof(Cell);
if ((l >> 6) == MaxDistance) if ((l >> DistancePos) == MaxDistance)
addr += sizeof(Cell); addr += sizeof(Cell);
addr += len; addr += len;

@ -73,8 +73,11 @@ public:
/** "Immediate" marker bit for a word's definition. */ /** "Immediate" marker bit for a word's definition. */
constexpr static Cell Immediate = (1 << 5); constexpr static Cell Immediate = (1 << 5);
constexpr static Cell DistancePos = 6;
/** Maximum "short" distance between two definitions. */ /** Maximum "short" distance between two definitions. */
constexpr static Cell MaxDistance = (1 << (sizeof(Cell) * 8 - 6)) - 1; constexpr static Cell MaxDistance = (1 << (sizeof(Cell) * 8 - DistancePos)) - 1;
/** Returns the value of the cell at the given address. */ /** Returns the value of the cell at the given address. */
virtual Cell read(Addr) const noexcept = 0; virtual Cell read(Addr) const noexcept = 0;

@ -124,17 +124,7 @@ LIBALEE_SECTION
void Parser::processLiteral(State& state, Cell value) void Parser::processLiteral(State& state, Cell value)
{ {
if (state.compiling()) { if (state.compiling()) {
constexpr auto ins = CoreWords::token("_lit"); state.dict.add(CoreWords::token("_lit"));
// Literal compression: opcodes between WordCount and Begin are unused,
// so we assign literals to them to save space. Opcode "WordCount"
// pushes zero to the stack, "WordCount + 1" pushes a one, etc.
const Cell maxlit = Dictionary::Begin - CoreWords::WordCount;
if (value >= 0 && value < maxlit)
value += CoreWords::WordCount;
else
state.dict.add(ins);
state.dict.add(value); state.dict.add(value);
} else { } else {
state.push(value); state.push(value);

@ -54,8 +54,10 @@ Error State::execute(Addr addr)
if (context.ip >= Dictionary::Begin) { if (context.ip >= Dictionary::Begin) {
// longjmp will exit this loop. // longjmp will exit this loop.
for (;;) for (;;) {
context.ip += sizeof(Cell);
CoreWords::run(dict.read(context.ip), *this); CoreWords::run(dict.read(context.ip), *this);
}
} else { } else {
// addr was a CoreWord, all done now. // addr was a CoreWord, all done now.
context.ip = 0; context.ip = 0;

@ -28,6 +28,8 @@
#include <csetjmp> #include <csetjmp>
#include <cstddef> #include <cstddef>
//#define verify(C, E)
/** /**
* Size of the primary data stack, number of cells. * Size of the primary data stack, number of cells.
*/ */
@ -179,6 +181,8 @@ public:
return dict.read(context.ip); return dict.read(context.ip);
} }
//#undef verify
/** /**
* Asserts the given condition is true, longjmp-ing if false. * Asserts the given condition is true, longjmp-ing if false.
* Used as an exception handler and the method of exiting execution. * Used as an exception handler and the method of exiting execution.

Loading…
Cancel
Save