move main execution to State; bring back verify()

optimize
Clyne 1 year ago
parent 3dc947a757
commit 4b50a9fafe
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

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

@ -9,10 +9,10 @@
5 sys fib 5 sys >r 2drop r> ; 5 sys fib 5 sys >r 2drop r> ;
variable avg 0 avg ! variable avg 0 avg !
2000 constant iters 1000 constant iters
: bench ( -- ) : bench ( -- )
iters 0 do 100 fibbench avg +! loop iters 0 do 1000 fibbench avg +! loop
avg @ iters / avg ! ; avg @ iters / avg ! ;
bench ." avg time: " avg @ . ." us" cr bench ." avg time: " avg @ . ." us" cr

@ -37,21 +37,12 @@ void find(State& state, Word word)
state.push(imm); state.push(imm);
} }
void CoreWords::run(Cell ins, State& state) bool CoreWords::run(Cell ins, State& state)
{ {
DoubleCell dcell; DoubleCell dcell;
Addr index = ins;
auto& ip = state.ip(); auto& ip = state.ip();
execute: switch (ins) {
if (index >= Dictionary::Begin) {
// must be calling a defined subroutine
state.pushr(ip);
ip = index;
return;
} else if (index >= WordCount) {
state.push(static_cast<Cell>(index - WordCount));
} else switch (index) {
case 0: // _lit case 0: // _lit
state.push(state.beyondip()); state.push(state.beyondip());
break; break;
@ -149,8 +140,7 @@ execute:
find(state, state.dict.input()); find(state, state.dict.input());
break; break;
case 24: // execute case 24: // execute
index = state.pop(); return true;
goto execute;
case 25: // exit case 25: // exit
ip = state.popr(); ip = state.popr();
if (ip == 0) if (ip == 0)
@ -180,8 +170,8 @@ execute:
} }
[[fallthrough]]; [[fallthrough]];
case 28: // _jmp case 28: // _jmp
ip = state.beyondip(); ip = static_cast<Addr>(state.beyondip() - sizeof(Cell));
return; break;
case 29: // depth case 29: // depth
state.push(static_cast<Cell>(state.size())); state.push(static_cast<Cell>(state.size()));
break; break;
@ -191,7 +181,7 @@ execute:
case 31: // _in case 31: // _in
state.input(); state.input();
break; break;
case 32: // _ex case 32: // _ev
{ {
const auto st = state.save(); const auto st = state.save();
ip = 0; ip = 0;
@ -235,7 +225,7 @@ execute:
break; break;
} }
ip += sizeof(Cell); return false;
} }
Cell CoreWords::findi(State& state, Word word) Cell CoreWords::findi(State& state, Word word)

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

@ -42,17 +42,39 @@ void State::load(const State::Context& ctx)
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) Error State::execute(Addr addr)
{ {
auto stat = static_cast<Error>(setjmp(context.jmpbuf)); auto stat = static_cast<Error>(setjmp(context.jmpbuf));
if (stat == Error::none) { if (stat == Error::none) {
CoreWords::run(addr, *this); context.ip = 0;
execute1(addr);
if (context.ip >= Dictionary::Begin) { if (context.ip >= Dictionary::Begin) {
// longjmp will exit this loop. // longjmp will exit this loop.
for (;;) for (;;)
CoreWords::run(dict.read(context.ip), *this); execute1(dict.read(context.ip));
} else { } else {
// addr was a CoreWord, all done now. // addr was a CoreWord, all done now.
context.ip = 0; context.ip = 0;

@ -25,8 +25,6 @@
#include <csetjmp> #include <csetjmp>
#include <cstddef> #include <cstddef>
#define verify(C, E)
constexpr unsigned DataStackSize = 64; constexpr unsigned DataStackSize = 64;
constexpr unsigned ReturnStackSize = 64; constexpr unsigned ReturnStackSize = 64;
@ -52,6 +50,7 @@ public:
* Encountering an error will cause this function to exit immediately. * Encountering an error will cause this function to exit immediately.
*/ */
Error execute(Addr); Error execute(Addr);
void execute1(Addr);
/** /**
* Clears the data and return stacks, sets ip to zero, and clears the * Clears the data and return stacks, sets ip to zero, and clears the
@ -135,10 +134,10 @@ public:
return dict.read(context.ip); return dict.read(context.ip);
} }
// inline void verify(bool condition, Error error) { inline void verify(bool condition, Error error) {
// if (!condition) if (!condition)
// std::longjmp(context.jmpbuf, static_cast<int>(error)); std::longjmp(context.jmpbuf, static_cast<int>(error));
// } }
private: private:
InputFunc inputfunc; // User-provided function to collect "stdin" input. InputFunc inputfunc; // User-provided function to collect "stdin" input.

Loading…
Cancel
Save