Skip to content

Commit

Permalink
Review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfallin committed Oct 24, 2024
1 parent 5798a9e commit 6929a00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ jobs:
smoke-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Download and install wasi-sdk
run: |
cd /opt
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-24/wasi-sdk-24.0-x86_64-linux.tar.gz
tar zxvf wasi-sdk-24.0-x86_64-linux.tar.gz
ln -s wasi-sdk-24.0-x86_64-linux wasi-sdk
ls /opt/wasi-sdk
- name: Download and install wasmtime
run: |
cd /opt
Expand Down
47 changes: 26 additions & 21 deletions tests/simple/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <wizer.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

WIZER_DEFAULT_INIT();
WEVAL_DEFINE_GLOBALS();
Expand Down Expand Up @@ -37,7 +38,7 @@ struct State {
};

template<bool Specialized>
bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
uint32_t Interpret(const Inst* insts, uint32_t ninsts, State* state) {
uint32_t pc = 0;
uint32_t steps = 0;
uint32_t* opstack = state->opstack;
Expand All @@ -57,67 +58,67 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
switch (inst->opcode) {
case PushConst:
if (sp + 1 > OPSTACK_SIZE) {
return false;
return 0;
}
opstack[sp++] = inst->imm;
break;
case Drop:
if (sp == 0) {
return false;
return 0;
}
sp--;
break;
case Dup:
if (sp + 1 > OPSTACK_SIZE) {
return false;
return 0;
}
if (sp == 0) {
return false;
return 0;
}
opstack[sp] = opstack[sp - 1];
sp++;
break;
case GetLocal:
if (sp + 1 > OPSTACK_SIZE) {
return false;
return 0;
}
if (inst->imm >= LOCAL_SIZE) {
return false;
return 0;
}
opstack[sp++] = locals[inst->imm];
break;
case SetLocal:
if (sp == 0) {
return false;
return 0;
}
if (inst->imm >= LOCAL_SIZE) {
return false;
return 0;
}
locals[inst->imm] = opstack[--sp];
break;
case Add:
if (sp < 2) {
return false;
return 0;
}
opstack[sp - 2] += opstack[sp - 1];
sp--;
break;
case Sub:
if (sp < 2) {
return false;
return 0;
}
opstack[sp - 2] -= opstack[sp - 1];
sp--;
break;
case Print:
if (sp == 0) {
return false;
return 0;
}
printf("%u\n", opstack[--sp]);
break;
case Goto:
if (inst->imm >= ninsts) {
return false;
return 0;
}
pc = inst->imm;
if (Specialized) {
Expand All @@ -126,10 +127,10 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
break;
case GotoIf:
if (sp == 0) {
return false;
return 0;
}
if (inst->imm >= ninsts) {
return false;
return 0;
}
sp--;
if (opstack[sp] != 0) {
Expand All @@ -150,22 +151,24 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
}

printf("Exiting after %d steps at PC %d.\n", steps, pc);
return true;
return steps;
}

static const uint32_t kIters = 10000000;
Inst prog[] = {
Inst(PushConst, 0),
Inst(Dup),
Inst(PushConst, 10000000),
Inst(PushConst, kIters),
Inst(Sub),
Inst(GotoIf, 6),
Inst(Exit),
Inst(PushConst, 1),
Inst(Add),
Inst(Goto, 1),
};
static const uint32_t kExpectedSteps = 7*kIters + 6;

typedef bool (*InterpretFunc)(const Inst* insts, uint32_t ninsts, State* state);
typedef uint32_t (*InterpretFunc)(const Inst* insts, uint32_t ninsts, State* state);

WEVAL_DEFINE_TARGET(1, Interpret<true>);

Expand All @@ -177,17 +180,18 @@ struct Func {
Func(const Inst* insts_, uint32_t ninsts_)
: insts(insts_), ninsts(ninsts_), specialized(nullptr) {
printf("ctor: ptr %p\n", &specialized);
weval::weval(
auto* req = weval::weval(
&specialized,
&Interpret<true>,
1,
0,
weval::SpecializeMemory<const Inst*>(insts, ninsts * sizeof(Inst)),
weval::Specialize(ninsts),
weval::Runtime<State*>());
assert(req);
}

bool invoke(State* state) {
uint32_t invoke(State* state) {
printf("Inspecting func ptr at: %p -> %p (size %lu)\n", &specialized, specialized, sizeof(specialized));
if (specialized) {
printf("Calling specialized function: %p\n", specialized);
Expand All @@ -201,6 +205,7 @@ Func prog_func(prog, sizeof(prog)/sizeof(Inst));

int main(int argc, char** argv) {
State* state = (State*)calloc(sizeof(State), 1);
prog_func.invoke(state);
uint32_t steps = prog_func.invoke(state);
assert(kExpectedSteps == steps);
fflush(stdout);
}

0 comments on commit 6929a00

Please sign in to comment.