Compare commits

...

3 Commits

@ -20,6 +20,7 @@
#include "splitmemdict.hpp"
#include <charconv>
#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>
@ -83,6 +84,8 @@ static void load(State& state)
void user_sys(State& state)
{
static bool start = false;
static decltype(std::chrono::high_resolution_clock::now()) last;
char buf[32] = {0};
switch (state.pop()) {
@ -103,6 +106,20 @@ void user_sys(State& state)
case 4: // load
load(state);
break;
case 5: // time
if (!start) {
start = true;
last = std::chrono::high_resolution_clock::now();
} else {
start = false;
auto diff = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - last);
state.push((Cell)diff.count());
}
break;
case 6: // double-add
{ auto sum = state.popd() + state.popd(); state.pushd(sum); }
break;
default:
break;
}

@ -20,6 +20,7 @@
#include "memdict.hpp"
#include <charconv>
#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>
@ -82,6 +83,8 @@ static void load(State& state)
void user_sys(State& state)
{
static std::chrono::time_point<std::chrono::high_resolution_clock> last;
static bool start = false;
char buf[32] = {0};
switch (state.pop()) {
@ -102,6 +105,20 @@ void user_sys(State& state)
case 4: // load
load(state);
break;
case 5: // time
if (!start) {
start = true;
last = std::chrono::high_resolution_clock::now();
} else {
start = false;
auto diff = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - last);
state.push((Cell)diff.count());
}
break;
case 6: // double-add
{ auto sum = state.popd() + state.popd(); state.pushd(sum); }
break;
default:
break;
}

@ -51,8 +51,8 @@
: else ['] _jmp , here 0 , swap here swap ! ; imm
: postpone _' dup 0 = if exit then
1 = swap ['] _lit , , if ['] execute ,
else ['] , , then ; imm
1 = swap ['] _lit , , if ['] execute
else ['] , then , ; imm
: over 1 pick ;
: rot >r swap r> swap ;

@ -0,0 +1,19 @@
: fib ( n -- d )
>r 0 dup 1 0 r> 0 do
2dup 2>r 2swap 6 sys 2r> 2swap loop 2drop ;
: fibtest ( n -- )
0 do i fib <# #s #> type space loop ;
: fibbench ( n -- )
5 sys fib 5 sys >r 2drop r> ;
variable avg 0 avg !
1000 constant iters
: bench ( -- )
iters 0 do 1000 fibbench avg +! loop
avg @ iters / avg ! ;
bench ." avg time: " avg @ . ." us" cr
bye

@ -37,34 +37,12 @@ void find(State& state, Word word)
state.push(imm);
}
void CoreWords::run(Cell ins, State& state)
bool CoreWords::run(Cell ins, State& state)
{
Cell cell;
DoubleCell dcell;
Addr index = ins;
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) {
// must be calling a defined subroutine
state.pushr(ip);
ip = index;
return;
} else switch (index) {
switch (ins) {
case 0: // _lit
state.push(state.beyondip());
break;
@ -84,27 +62,25 @@ execute:
user_sys(state);
break;
case 6: // add
cell = state.pop();
state.top() += cell;
{ auto& cell = state.pop(); state.top() += cell; }
break;
case 7: // sub
cell = state.pop();
state.top() -= cell;
{ auto& cell = state.pop(); state.top() -= cell; }
break;
case 8: // mul ( n n -- d )
cell = state.pop();
{ auto& cell = state.pop();
dcell = state.pop() * cell;
pushd(state, dcell);
state.pushd(dcell); }
break;
case 9: // div ( d n -- n )
cell = state.pop();
dcell = popd(state);
state.push(static_cast<Cell>(dcell / cell));
{ auto& cell = state.pop();
dcell = state.popd();
state.push(static_cast<Cell>(dcell / cell)); }
break;
case 10: // mod ( d n -- n )
cell = state.pop();
dcell = popd(state);
state.push(static_cast<Cell>(dcell % cell));
{ auto& cell = state.pop();
dcell = state.popd();
state.push(static_cast<Cell>(dcell % cell)); }
break;
case 11: // peek
if (state.pop())
@ -113,11 +89,11 @@ execute:
state.push(state.dict.readbyte(state.pop()));
break;
case 12: // poke
cell = state.pop();
{ auto& cell = state.pop();
if (auto addr = state.pop(); cell)
state.dict.write(addr, state.pop());
else
state.dict.writebyte(addr, state.pop() & 0xFFu);
state.dict.writebyte(addr, state.pop() & 0xFFu); }
break;
case 13: // pushr
state.pushr(state.pop());
@ -126,32 +102,29 @@ execute:
state.push(state.popr());
break;
case 15: // equal
cell = state.pop();
state.top() = state.top() == cell ? -1 : 0;
{ auto& cell = state.pop();
state.top() = state.top() == cell ? -1 : 0; }
break;
case 16: // lt
cell = state.pop();
state.top() = state.top() < cell ? -1 : 0;
{ auto& cell = state.pop();
state.top() = state.top() < cell ? -1 : 0; }
break;
case 17: // and
cell = state.pop();
state.top() &= cell;
{ auto& cell = state.pop(); state.top() &= cell; }
break;
case 18: // or
cell = state.pop();
state.top() |= cell;
{ auto& cell = state.pop(); state.top() |= cell; }
break;
case 19: // xor
cell = state.pop();
state.top() ^= cell;
{ auto& cell = state.pop(); state.top() ^= cell; }
break;
case 20: // shl
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
{ auto& cell = state.pop();
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell); }
break;
case 21: // shr
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
{ auto& cell = state.pop();
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell); }
break;
case 22: // colon
state.push(state.dict.alignhere());
@ -167,24 +140,28 @@ execute:
find(state, state.dict.input());
break;
case 24: // execute
index = state.pop();
goto execute;
return true;
case 25: // exit
ip = state.popr();
state.verify(ip != 0, Error::exit);
if (ip == 0)
state.exit();
break;
case 26: // semic
state.dict.add(findi("exit"));
state.compiling(false);
cell = state.pop();
{
auto& cell = state.pop();
dcell = cell - state.dict.latest();
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
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.write(cell,
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
state.dict.latest(cell);
}
break;
case 27: // _jmp0
if (state.pop()) {
@ -193,8 +170,8 @@ execute:
}
[[fallthrough]];
case 28: // _jmp
ip = state.beyondip();
return;
ip = static_cast<Addr>(state.beyondip() - sizeof(Cell));
break;
case 29: // depth
state.push(static_cast<Cell>(state.size()));
break;
@ -204,7 +181,7 @@ execute:
case 31: // _in
state.input();
break;
case 32: // _ex
case 32: // _ev
{
const auto st = state.save();
ip = 0;
@ -213,29 +190,30 @@ execute:
}
break;
case 33: // find
cell = state.pop();
{ auto& cell = state.pop();
find(state,
Word::fromLength(static_cast<Addr>(cell + 1),
state.dict.readbyte(cell)));
state.dict.readbyte(cell))); }
break;
case 34: // _uma
{
const auto plus = state.pop();
cell = state.pop();
dcell = popd(state);
const auto& plus = state.pop();
const auto& cell = state.pop();
dcell = state.popd();
dcell *= static_cast<Addr>(cell);
dcell += static_cast<Addr>(plus);
pushd(state, dcell);
state.pushd(dcell);
}
break;
case 35: // u<
cell = state.pop();
{ auto& cell = state.pop();
state.top() = static_cast<Addr>(state.top()) <
static_cast<Addr>(cell) ? -1 : 0;
static_cast<Addr>(cell) ? -1 : 0; }
break;
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) %
@ -243,13 +221,11 @@ execute:
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) /
static_cast<Addr>(cell)));
break;
default:
state.push(ins - WordCount);
}
break;
}
ip += sizeof(Cell);
return false;
}
Cell CoreWords::findi(State& state, Word word)

@ -48,7 +48,7 @@ public:
/**
* Executes the given CoreWord execution token using the given state.
*/
static void run(Cell, State&);
static bool run(Cell, State&);
constexpr static char wordsarr[] =
"_lit\0drop\0dup\0swap\0pick\0sys\0"

@ -42,17 +42,39 @@ void State::load(const State::Context& ctx)
context = ctx;
}
void State::execute1(Addr ins)
{
repeat:
if (ins >= Dictionary::Begin) [[likely]] {
// Subroutine call
pushr(context.ip);
context.ip = ins;
} else {
if (ins < CoreWords::WordCount) {
if (CoreWords::run(ins, *this)) [[unlikely]] {
ins = pop();
goto repeat;
}
} else {
push(static_cast<Cell>(ins - CoreWords::WordCount));
}
context.ip += sizeof(Cell);
}
}
Error State::execute(Addr addr)
{
auto stat = static_cast<Error>(setjmp(context.jmpbuf));
if (stat == Error::none) {
CoreWords::run(addr, *this);
context.ip = 0;
execute1(addr);
if (context.ip >= Dictionary::Begin) {
// longjmp will exit this loop.
for (;;)
CoreWords::run(dict.read(context.ip), *this);
execute1(dict.read(context.ip));
} else {
// addr was a CoreWord, all done now.
context.ip = 0;

@ -50,6 +50,7 @@ public:
* Encountering an error will cause this function to exit immediately.
*/
Error execute(Addr);
void execute1(Addr);
/**
* Clears the data and return stacks, sets ip to zero, and clears the
@ -57,6 +58,10 @@ public:
*/
void reset();
void exit() {
std::longjmp(context.jmpbuf, static_cast<int>(Error::exit));
}
Addr& ip() noexcept {
return context.ip;
}
@ -87,31 +92,42 @@ public:
*dsp++ = value;
}
inline Cell pop() {
inline const Cell& pop() {
verify(dsp > dstack, Error::pop);
return *--dsp;
}
inline DoubleCell popd() {
DoubleCell dcell = pop();
dcell <<= sizeof(Cell) * 8;
dcell |= static_cast<Addr>(pop());
return dcell;
}
inline void pushd(DoubleCell d) {
push(static_cast<Cell>(d));
push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
}
inline void pushr(Cell value) {
verify(rsp < rstack + ReturnStackSize, Error::pushr);
*rsp++ = value;
}
inline Cell popr() {
inline const Cell& popr() {
verify(rsp > rstack, Error::popr);
return *--rsp;
}
inline Cell& top() {
verify(dsp > dstack, Error::top);
return *(dsp - 1);
}
inline Cell& pick(std::size_t i) {
verify(dsp - i > dstack, Error::pick);
return *(dsp - i - 1);
}
inline Cell& top() {
return pick(0);
}
// Advances the instruction pointer and returns that cell's contents.
inline Cell beyondip() {
context.ip += sizeof(Cell);

@ -36,7 +36,7 @@ class SplitMemDict : public Dictionary
uint8_t extra[Dictionary::Begin];
Addr convertAddress(Addr addr) const noexcept {
return addr < RON ? addr : static_cast<Addr>(addr - RON);
return static_cast<Addr>(addr - (addr >= RON) * RON);
}
public:

Loading…
Cancel
Save