|
|
@ -39,31 +39,17 @@ void find(State& state, Word word)
|
|
|
|
|
|
|
|
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Cell cell;
|
|
|
|
|
|
|
|
DoubleCell dcell;
|
|
|
|
DoubleCell dcell;
|
|
|
|
|
|
|
|
const Addr index = ins;
|
|
|
|
Addr index = ins;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto& ip = state.ip();
|
|
|
|
auto& ip = state.ip();
|
|
|
|
|
|
|
|
|
|
|
|
auto popd = [](State& s) {
|
|
|
|
|
|
|
|
DoubleCell dcell = s.pop();
|
|
|
|
|
|
|
|
dcell <<= sizeof(Cell) * 8;
|
|
|
|
|
|
|
|
dcell |= static_cast<Addr>(s.pop());
|
|
|
|
|
|
|
|
return dcell;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto pushd = [](State& s, DoubleCell d) {
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d));
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
execute:
|
|
|
|
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
// must be calling a defined subroutine
|
|
|
|
// must be calling a defined subroutine
|
|
|
|
state.pushr(ip);
|
|
|
|
state.pushr(ip);
|
|
|
|
ip = index;
|
|
|
|
ip = index;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (index >= WordCount) {
|
|
|
|
|
|
|
|
state.push(index - WordCount);
|
|
|
|
} else switch (index) {
|
|
|
|
} else switch (index) {
|
|
|
|
case 0: // _lit
|
|
|
|
case 0: // _lit
|
|
|
|
state.push(state.beyondip());
|
|
|
|
state.push(state.beyondip());
|
|
|
@ -84,27 +70,25 @@ execute:
|
|
|
|
user_sys(state);
|
|
|
|
user_sys(state);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 6: // add
|
|
|
|
case 6: // add
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop(); state.top() += cell; }
|
|
|
|
state.top() += cell;
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 7: // sub
|
|
|
|
case 7: // sub
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop(); state.top() -= cell; }
|
|
|
|
state.top() -= cell;
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 8: // mul ( n n -- d )
|
|
|
|
case 8: // mul ( n n -- d )
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
dcell = state.pop() * cell;
|
|
|
|
dcell = state.pop() * cell;
|
|
|
|
pushd(state, dcell);
|
|
|
|
state.pushd(dcell); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 9: // div ( d n -- n )
|
|
|
|
case 9: // div ( d n -- n )
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
dcell = popd(state);
|
|
|
|
dcell = state.popd();
|
|
|
|
state.push(static_cast<Cell>(dcell / cell));
|
|
|
|
state.push(static_cast<Cell>(dcell / cell)); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 10: // mod ( d n -- n )
|
|
|
|
case 10: // mod ( d n -- n )
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
dcell = popd(state);
|
|
|
|
dcell = state.popd();
|
|
|
|
state.push(static_cast<Cell>(dcell % cell));
|
|
|
|
state.push(static_cast<Cell>(dcell % cell)); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 11: // peek
|
|
|
|
case 11: // peek
|
|
|
|
if (state.pop())
|
|
|
|
if (state.pop())
|
|
|
@ -113,11 +97,11 @@ execute:
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 12: // poke
|
|
|
|
case 12: // poke
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.dict.writebyte(addr, state.pop() & 0xFFu);
|
|
|
|
state.dict.writebyte(addr, state.pop() & 0xFFu); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 13: // pushr
|
|
|
|
case 13: // pushr
|
|
|
|
state.pushr(state.pop());
|
|
|
|
state.pushr(state.pop());
|
|
|
@ -126,32 +110,29 @@ execute:
|
|
|
|
state.push(state.popr());
|
|
|
|
state.push(state.popr());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 15: // equal
|
|
|
|
case 15: // equal
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
state.top() = state.top() == cell ? -1 : 0;
|
|
|
|
state.top() = state.top() == cell ? -1 : 0; }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 16: // lt
|
|
|
|
case 16: // lt
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
state.top() = state.top() < cell ? -1 : 0;
|
|
|
|
state.top() = state.top() < cell ? -1 : 0; }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 17: // and
|
|
|
|
case 17: // and
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop(); state.top() &= cell; }
|
|
|
|
state.top() &= cell;
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 18: // or
|
|
|
|
case 18: // or
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop(); state.top() |= cell; }
|
|
|
|
state.top() |= cell;
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 19: // xor
|
|
|
|
case 19: // xor
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop(); state.top() ^= cell; }
|
|
|
|
state.top() ^= cell;
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 20: // shl
|
|
|
|
case 20: // shl
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
|
|
|
|
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 21: // shr
|
|
|
|
case 21: // shr
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
|
|
|
|
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 22: // colon
|
|
|
|
case 22: // colon
|
|
|
|
state.push(state.dict.alignhere());
|
|
|
|
state.push(state.dict.alignhere());
|
|
|
@ -167,8 +148,8 @@ execute:
|
|
|
|
find(state, state.dict.input());
|
|
|
|
find(state, state.dict.input());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 24: // execute
|
|
|
|
case 24: // execute
|
|
|
|
index = state.pop();
|
|
|
|
ip = state.pop();
|
|
|
|
goto execute;
|
|
|
|
return;
|
|
|
|
case 25: // exit
|
|
|
|
case 25: // exit
|
|
|
|
ip = state.popr();
|
|
|
|
ip = state.popr();
|
|
|
|
state.verify(ip != 0, Error::exit);
|
|
|
|
state.verify(ip != 0, Error::exit);
|
|
|
@ -177,14 +158,18 @@ execute:
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.compiling(false);
|
|
|
|
state.compiling(false);
|
|
|
|
|
|
|
|
|
|
|
|
cell = state.pop();
|
|
|
|
{
|
|
|
|
dcell = cell - state.dict.latest();
|
|
|
|
auto& cell = state.pop();
|
|
|
|
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
|
|
|
|
dcell = cell - state.dict.latest();
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
|
|
|
|
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
|
|
|
|
dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell),
|
|
|
|
|
|
|
|
static_cast<Cell>(dcell));
|
|
|
|
|
|
|
|
dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
state.dict.write(cell,
|
|
|
|
|
|
|
|
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
|
|
|
|
|
|
|
|
state.dict.latest(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.dict.write(cell, (state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
|
|
|
|
|
|
|
|
state.dict.latest(cell);
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 27: // _jmp0
|
|
|
|
case 27: // _jmp0
|
|
|
|
if (state.pop()) {
|
|
|
|
if (state.pop()) {
|
|
|
@ -213,39 +198,38 @@ execute:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 33: // find
|
|
|
|
case 33: // find
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
find(state,
|
|
|
|
find(state,
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
state.dict.readbyte(cell)));
|
|
|
|
state.dict.readbyte(cell))); }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 34: // _uma
|
|
|
|
case 34: // _uma
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const auto plus = state.pop();
|
|
|
|
const auto& plus = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
const auto& cell = state.pop();
|
|
|
|
dcell = popd(state);
|
|
|
|
dcell = state.popd();
|
|
|
|
dcell *= static_cast<Addr>(cell);
|
|
|
|
dcell *= static_cast<Addr>(cell);
|
|
|
|
dcell += static_cast<Addr>(plus);
|
|
|
|
dcell += static_cast<Addr>(plus);
|
|
|
|
pushd(state, dcell);
|
|
|
|
state.pushd(dcell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 35: // u<
|
|
|
|
case 35: // u<
|
|
|
|
cell = state.pop();
|
|
|
|
{ auto& cell = state.pop();
|
|
|
|
state.top() = static_cast<Addr>(state.top()) <
|
|
|
|
state.top() = static_cast<Addr>(state.top()) <
|
|
|
|
static_cast<Addr>(cell) ? -1 : 0;
|
|
|
|
static_cast<Addr>(cell) ? -1 : 0; }
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 36: // um/mod
|
|
|
|
case 36: // um/mod
|
|
|
|
cell = state.pop();
|
|
|
|
{
|
|
|
|
dcell = popd(state);
|
|
|
|
const auto& cell = state.pop();
|
|
|
|
|
|
|
|
dcell = state.popd();
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
|
|
|
|
static_cast<DoubleAddr>(dcell) %
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
static_cast<DoubleAddr>(dcell) %
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
break;
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
state.push(ins - WordCount);
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|