Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔬 memory primitive prototype #238

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (BUILD_EMULATOR)
src/Primitives/emulated.cpp
src/Interpreter/instructions.cpp
src/Interpreter/interpreter.cpp
src/Interpreter/proxied.cpp
src/Memory/mem.cpp
src/Utils/util.cpp
src/Utils/util_arduino.cpp
Expand Down Expand Up @@ -86,6 +87,7 @@ if (BUILD_UNITTEST)
src/Primitives/emulated.cpp
src/Interpreter/instructions.cpp
src/Interpreter/interpreter.cpp
src/Interpreter/proxied.cpp
src/Memory/mem.cpp
src/Utils/util.cpp
src/Utils/util_arduino.cpp
Expand Down
1 change: 1 addition & 0 deletions platforms/ESP-IDF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(SOURCE_FILES
../../src/Debug/debugger.cpp
../../src/Interpreter/instructions.cpp
../../src/Interpreter/interpreter.cpp
../../src/Interpreter/proxied.cpp
../../src/Memory/mem.cpp
../../src/Primitives/idf.cpp
../../src/Edward/proxy.cpp
Expand Down
18 changes: 17 additions & 1 deletion src/Debug/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../../lib/json/single_include/nlohmann/json.hpp"
#endif

#include "../Interpreter/proxied.h"
#include "../Memory/mem.h"
#include "../Utils//util.h"
#include "../Utils/macros.h"
Expand Down Expand Up @@ -329,6 +330,10 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
this->dumpCallbackmapping();
free(interruptData);
break;
case interruptStore:
this->receiveStore(m, interruptData + 1);
free(interruptData);
break;
default:
// handle later
this->channel->write("COULD not parse interrupt data!\n");
Expand Down Expand Up @@ -1191,6 +1196,8 @@ uintptr_t Debugger::readPointer(uint8_t **data) {

void Debugger::proxify() {
WARDuino::instance()->program_state = PROXYhalt;
delete WARDuino::instance()->interpreter;
WARDuino::instance()->interpreter = new Proxied();
this->proxy = new Proxy(); // TODO delete
}

Expand All @@ -1209,7 +1216,7 @@ void Debugger::handleProxyCall(Module *m, RunningState *program_state,
StackValue *args = Proxy::readRFCArgs(func, data);
dbg_trace("Enqueuing callee %" PRIu32 "\n", func->fidx);

auto *rfc = new RFC(fidx, func->type, args);
auto *rfc = new RFC(m, fidx, func->type, args);
this->proxy->pushRFC(m, rfc);
}

Expand Down Expand Up @@ -1278,6 +1285,15 @@ void Debugger::updateCallbackmapping(Module *m, const char *data) {
}
}

void Debugger::receiveStore(Module *m, uint8_t *interruptData) {
uint8_t *pos = interruptData;
uint32_t addr = read_LEB_32(&pos);
auto *sval = (StackValue *)malloc(sizeof(struct StackValue));
deserialiseStackValue(pos, true, sval);
m->warduino->interpreter->store(m, sval->value_type, addr, *sval);
free(sval);
}

// Stop the debugger
void Debugger::stop() {
if (this->channel != nullptr) {
Expand Down
14 changes: 5 additions & 9 deletions src/Debug/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
#include <vector>

#include "../Edward/proxy.h"
#include "../Edward/proxy_supervisor.h"
#include "../Utils/sockets.h"

struct Module;
struct Block;
struct StackValue;

enum operation {
STORE = 0,
LOAD = 1,
};

enum RunningState {
WARDUINOinit,
WARDUINOrun,
Expand Down Expand Up @@ -89,11 +83,13 @@ enum InterruptTypes {
interruptRecvCallbackmapping = 0x75,

// Operations
interruptStore = 0xa0,
interruptStored = 0xa1,
interruptStore = 0x80,
interruptStored = 0x81,

};

class ProxySupervisor;

class Debugger {
private:
std::deque<uint8_t *> debugMessages = {};
Expand Down Expand Up @@ -184,7 +180,7 @@ class Debugger {

static void updateCallbackmapping(Module *m, const char *interruptData);

bool operation(Module *m, operation op);
void receiveStore(Module *m, uint8_t *interruptData);

public:
// Public fields
Expand Down
4 changes: 2 additions & 2 deletions src/Edward/RFC.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "RFC.h"

RFC::RFC(uint32_t id, Type *t_type, StackValue *t_args)
: fidx(id), args(t_args), type(t_type) {}
RFC::RFC(Module *m, uint32_t id, Type *t_type, StackValue *t_args)
: m(m), fidx(id), args(t_args), type(t_type) {}
5 changes: 4 additions & 1 deletion src/Edward/RFC.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <cstdint>

#include "../WARDuino/internals.h"
struct StackValue;
struct Type;

Expand All @@ -11,6 +13,7 @@ struct SerializeData {

class RFC {
public:
Module *m;
const uint32_t fidx;
StackValue *args;
const Type *type;
Expand All @@ -20,5 +23,5 @@ class RFC {
char *exception;
uint16_t exception_size;

RFC(uint32_t id, Type *t_type, StackValue *t_args = nullptr);
RFC(Module *m, uint32_t id, Type *t_type, StackValue *t_args = nullptr);
};
48 changes: 14 additions & 34 deletions src/Edward/proxy_supervisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ Event *parseJSON(char *buff) {
return new Event(*parsed.find("topic"), payload);
}

ProxySupervisor::ProxySupervisor(Channel *duplex, std::mutex *mutex) {
ProxySupervisor::ProxySupervisor(Channel *duplex, std::mutex *mutex)
: Debugger(duplex) {
debug("Starting supervisor.\n");
this->channel = duplex;
this->mutex = mutex;
this->thread = std::thread(runSupervisor, this);
this->proxyResult = nullptr;
Expand All @@ -71,39 +71,21 @@ bool isReply(nlohmann::basic_json<> parsed) {
}

void ProxySupervisor::listenToSocket() {
char _char;
uint32_t buf_idx = 0;
const uint32_t start_size = 1024;
uint32_t current_size = start_size;
char *buffer = (char *)malloc(start_size);
uint8_t message[1024] = {0};
ssize_t readAmount;

this->channel->open();

dbg_info("Proxy supervisor listening to remote device...\n");
while (continuing(this->mutex)) {
readAmount = this->channel->read(&_char, 1);
readAmount = this->channel->read(message, 1024);
if (readAmount == -1) {
printf("Proxy supervisor shutting down.\n");
exit(-1);
}
if (readAmount > 0) {
// increase buffer size if needed
if (current_size <= (buf_idx + 1)) {
char *new_buff = (char *)malloc(current_size + start_size);
memcpy((void *)new_buff, (void *)buffer, current_size);
free(buffer);
buffer = new_buff;
current_size += start_size;
printf("increasing PushClient's buffer size to %" PRId32 "\n",
current_size);
}
buffer[buf_idx++] = _char;
// manual null-termination is needed because parseJSON does not use
// first len argument
buffer[buf_idx] = '\0';
try {
nlohmann::basic_json<> parsed = nlohmann::json::parse(buffer);
nlohmann::basic_json<> parsed = nlohmann::json::parse(message);
debug("parseJSON: %s\n", parsed.dump().c_str());

if (isEvent(parsed)) {
Expand All @@ -116,13 +98,9 @@ void ProxySupervisor::listenToSocket() {
this->hasReplied = true;
this->proxyResult = parsed;
}

buf_idx = 0;
} catch (const nlohmann::detail::parse_error &e) {
if (_char == '\n') {
// discard buffer
buf_idx = 0;
}
printf("Non RFC call: %s", message);
WARDuino::instance()->handleInterrupt(readAmount, message);
}
}
}
Expand All @@ -134,9 +112,11 @@ bool ProxySupervisor::send(
return n == size;
}

nlohmann::basic_json<> ProxySupervisor::readReply() {
while (!this->hasReplied)
;
nlohmann::basic_json<> ProxySupervisor::readReply(RFC *rfc) {
while (!this->hasReplied) {
WARDuino::instance()->debugger->checkDebugMessages(
rfc->m, &WARDuino::instance()->program_state);
}
WARDuino::instance()->debugger->channel->write("read reply: succeeded\n");
this->hasReplied = false;
return this->proxyResult;
Expand Down Expand Up @@ -220,7 +200,7 @@ struct SerializeData *ProxySupervisor::serializeRFC(RFC *callee) {
}

void ProxySupervisor::deserializeRFCResult(RFC *rfc) {
nlohmann::basic_json<> call_result = this->readReply(); // blocking
nlohmann::basic_json<> call_result = this->readReply(rfc); // blocking
rfc->success = *call_result.find("success");

if (rfc->type->result_count == 0) {
Expand Down Expand Up @@ -278,7 +258,7 @@ bool ProxySupervisor::call(RFC *callee) {
char cmdBuffer[10] = "";
int cmdBufferLen = 0;
sprintf(cmdBuffer, "%x\n%n", interruptDUMPCallbackmapping, &cmdBufferLen);
WARDuino::instance()->debugger->supervisor->send(cmdBuffer, cmdBufferLen);
this->send(cmdBuffer, cmdBufferLen);
this->deserializeRFCResult(callee);
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Edward/proxy_supervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <thread>

#include "../Debug/debugger.h"
#include "../Utils/sockets.h"
#include "RFC.h"
#ifndef ARDUINO
Expand All @@ -15,9 +16,8 @@
#endif
#include "sys/types.h"

class ProxySupervisor {
class ProxySupervisor : public Debugger {
private:
Channel *channel;
std::mutex *mutex;
std::set<uint32_t> *proxied = new std::set<uint32_t>();

Expand All @@ -35,7 +35,7 @@ class ProxySupervisor {
void listenToSocket();

bool send(void *t_buffer, int t_size);
nlohmann::basic_json<> readReply();
nlohmann::basic_json<> readReply(RFC *rfc);

bool call(RFC *callee);

Expand Down
4 changes: 2 additions & 2 deletions src/Interpreter/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ bool proxy_call(Module *m, uint32_t fidx) {
if (type->param_count > 0) {
m->sp -= type->param_count;
StackValue *args = &m->stack[m->sp + 1];
rfc = new RFC(fidx, type, args);
rfc = new RFC(m, fidx, type, args);
} else {
rfc = new RFC(fidx, type);
rfc = new RFC(m, fidx, type);
}

if (!supervisor->call(rfc)) {
Expand Down
6 changes: 2 additions & 4 deletions src/Interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ uint32_t STORE_SIZE[] = {4, 8, 4, 8, 1, 2, 1, 2, 4};

bool Interpreter::store(Module *m, uint8_t type, uint32_t addr,
StackValue &sval) {
if (m->warduino->debugger->isProxy()) {
return m->warduino->debugger;
}

uint8_t *maddr, *mem_end;
uint32_t size = STORE_SIZE[abs(type - I32)];
bool overflow = false;
Expand Down Expand Up @@ -466,3 +462,5 @@ void Interpreter::report_overflow(Module *m, uint8_t *maddr) {
m->memory.bytes + m->memory.pages * (uint32_t)PAGE_SIZE, maddr);
sprintf(exception, "out of bounds memory access");
}

Interpreter::~Interpreter() {}
5 changes: 4 additions & 1 deletion src/Interpreter/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Interpreter {
public:
virtual ~Interpreter();

/**
* Push a new frame on to the call stack
* @param m module
Expand Down Expand Up @@ -48,7 +50,8 @@ class Interpreter {
/* Stateful operations
* ************************************************************************/

bool store(Module *m, uint8_t type, uint32_t addr, StackValue &sval);
virtual bool store(Module *m, uint8_t type, uint32_t addr,
StackValue &sval);

bool load(Module *m, uint8_t type, uint32_t addr, uint32_t offset);

Expand Down
21 changes: 21 additions & 0 deletions src/Interpreter/proxied.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "./proxied.h"

#include "../Debug/debugger.h"
#include "../Utils//util.h"

void send_leb(Channel *channel, uint32_t value, const char *end = "") {
uint8_t *buffer = write_LEB(value);
uint32_t size = size_leb(value);
for (int i = 0; i < size; ++i) {
channel->write("%02" PRIx8 "%s", buffer[i], end);
}
free(buffer);
}

bool Proxied::store(Module *m, uint8_t type, uint32_t addr, StackValue &sval) {
m->warduino->debugger->channel->write("%02" PRIx8, interruptStore);
send_leb(m->warduino->debugger->channel, addr);
send_leb(m->warduino->debugger->channel, 0);
send_leb(m->warduino->debugger->channel, sval.value.uint32, "\n");
return true;
}
6 changes: 6 additions & 0 deletions src/Interpreter/proxied.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "./interpreter.h"
class Proxied : public Interpreter {
public:
bool store(Module *m, uint8_t type, uint32_t addr,
StackValue &sval) override;
};
Loading
Loading