#include "splitmemdict.hpp"
#include <charconv>
+#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>
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()) {
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;
}
5 sys fib 5 sys >r 2drop r> ;
variable avg 0 avg !
-100 constant iters
+2000 constant iters
: bench ( -- )
iters 0 do 100 fibbench avg +! loop
void CoreWords::run(Cell ins, State& state)
{
DoubleCell dcell;
- const Addr index = ins;
+ Addr index = ins;
auto& ip = state.ip();
+execute:
if (index >= Dictionary::Begin) {
// must be calling a defined subroutine
state.pushr(ip);
ip = index;
return;
} else if (index >= WordCount) {
- state.push(index - WordCount);
+ state.push(static_cast<Cell>(index - WordCount));
} else switch (index) {
case 0: // _lit
state.push(state.beyondip());
find(state, state.dict.input());
break;
case 24: // execute
- ip = state.pop();
- return;
+ index = state.pop();
+ goto execute;
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"));
#include <csetjmp>
#include <cstddef>
+#define verify(C, E)
+
constexpr unsigned DataStackSize = 64;
constexpr unsigned ReturnStackSize = 64;
*/
void reset();
+ void exit() {
+ std::longjmp(context.jmpbuf, static_cast<int>(Error::exit));
+ }
+
Addr& ip() noexcept {
return context.ip;
}
return dict.read(context.ip);
}
- inline void verify(bool condition, Error error) {
- if (!condition)
- std::longjmp(context.jmpbuf, static_cast<int>(error));
- }
+// inline void verify(bool condition, Error error) {
+// if (!condition)
+// std::longjmp(context.jmpbuf, static_cast<int>(error));
+// }
private:
InputFunc inputfunc; // User-provided function to collect "stdin" input.
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: