fix execute; disable verify(); bench w/ standalone

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

@ -20,6 +20,7 @@
#include "splitmemdict.hpp" #include "splitmemdict.hpp"
#include <charconv> #include <charconv>
#include <chrono>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -83,6 +84,8 @@ static void load(State& state)
void user_sys(State& state) void user_sys(State& state)
{ {
static bool start = false;
static decltype(std::chrono::high_resolution_clock::now()) last;
char buf[32] = {0}; char buf[32] = {0};
switch (state.pop()) { switch (state.pop()) {
@ -103,6 +106,20 @@ void user_sys(State& state)
case 4: // load case 4: // load
load(state); load(state);
break; 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: default:
break; break;
} }

@ -9,7 +9,7 @@
5 sys fib 5 sys >r 2drop r> ; 5 sys fib 5 sys >r 2drop r> ;
variable avg 0 avg ! variable avg 0 avg !
100 constant iters 2000 constant iters
: bench ( -- ) : bench ( -- )
iters 0 do 100 fibbench avg +! loop iters 0 do 100 fibbench avg +! loop

@ -40,16 +40,17 @@ void find(State& state, Word word)
void CoreWords::run(Cell ins, State& state) void CoreWords::run(Cell ins, State& state)
{ {
DoubleCell dcell; DoubleCell dcell;
const Addr index = ins; Addr index = ins;
auto& ip = state.ip(); auto& ip = state.ip();
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) { } else if (index >= WordCount) {
state.push(index - WordCount); state.push(static_cast<Cell>(index - WordCount));
} else switch (index) { } else switch (index) {
case 0: // _lit case 0: // _lit
state.push(state.beyondip()); state.push(state.beyondip());
@ -148,11 +149,12 @@ void CoreWords::run(Cell ins, State& state)
find(state, state.dict.input()); find(state, state.dict.input());
break; break;
case 24: // execute case 24: // execute
ip = state.pop(); index = state.pop();
return; goto execute;
case 25: // exit case 25: // exit
ip = state.popr(); ip = state.popr();
state.verify(ip != 0, Error::exit); if (ip == 0)
state.exit();
break; break;
case 26: // semic case 26: // semic
state.dict.add(findi("exit")); state.dict.add(findi("exit"));

@ -25,6 +25,8 @@
#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;
@ -57,6 +59,10 @@ public:
*/ */
void reset(); void reset();
void exit() {
std::longjmp(context.jmpbuf, static_cast<int>(Error::exit));
}
Addr& ip() noexcept { Addr& ip() noexcept {
return context.ip; return context.ip;
} }
@ -129,10 +135,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.

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

Loading…
Cancel
Save