Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Debug/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#endif

#include "../Memory/mem.h"
#include "../Primitives/primitives.h"
#include "../Utils//util.h"
#include "../Utils/macros.h"
#include "../WARDuino/CallbackHandler.h"
Expand Down Expand Up @@ -922,7 +921,8 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray,
this->channel->write("%s", addComma ? "," : "");
this->channel->write("\"io\": [");
bool comma = false;
std::vector<IOStateElement *> external_state = get_io_state(m);
std::vector<IOStateElement *> external_state =
m->warduino->interpreter->get_io_state(m);
for (auto state_elem : external_state) {
this->channel->write("%s{", comma ? ", " : "");
this->channel->write(
Expand Down Expand Up @@ -1383,7 +1383,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) {
state_elem.output ? "output" : "input",
state_elem.value);
}
restore_external_state(m, external_state);
m->warduino->interpreter->restore_external_state(
m, external_state);
break;
}
case overridesState: {
Expand Down
57 changes: 57 additions & 0 deletions src/Interpreter/interpreter.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <set>

#include "../Utils/macros.h"
#include "../WARDuino/internals.h"

class Interpreter {
Expand Down Expand Up @@ -54,6 +57,60 @@ class Interpreter {

static void report_overflow(Module *m, uint8_t *maddr);

void register_primitive(const PrimitiveEntry &entry) {
primitives.push_back(entry);
}

//------------------------------------------------------
// resolving the primitives
//------------------------------------------------------
// ReSharper disable once CppDFAConstantFunctionResult
bool resolve_primitive(const char *symbol, Primitive *val) {
debug("Resolve primitive %s\n", symbol);

for (auto &primitive : primitives) {
// printf("Checking %s = %s \n", symbol, primitive.name);
if (!strcmp(symbol, primitive.name)) {
debug("FOUND PRIMITIVE\n");
*val = primitive.f;
return true;
}
}
FATAL("Could not find primitive %s \n", symbol);
return false; // unreachable
}

//------------------------------------------------------
// Restore external state when restoring a snapshot
//------------------------------------------------------
void restore_external_state(
Module *m, const std::vector<IOStateElement> &external_state) {
std::set<std::string> prim_names;
for (uint32_t i = 0; i < m->import_count; i++) {
prim_names.emplace(m->functions[i].import_field);
}

for (PrimitiveEntry &p : primitives) {
if (prim_names.find(p.name) != prim_names.end()) {
if (p.f_reverse) {
dbg_info("Reversing state for primitive %s\n", p.name);
p.f_reverse(m, external_state);
}
}
}
}

std::vector<IOStateElement *> get_io_state(Module *) const {
std::vector<IOStateElement *> ioState;
for (auto &primitive : primitives) {
if (primitive.f_serialize_state) {
primitive.f_serialize_state(ioState);
}
}
return ioState;
}

protected:
private:
std::vector<PrimitiveEntry> primitives;
};
Loading