Skip to content

Commit 1c2d4b7

Browse files
committed
Add interruptINVOKE
Move readArgs code from Proxy to util.cpp
1 parent 773e6a1 commit 1c2d4b7

File tree

5 files changed

+66
-47
lines changed

5 files changed

+66
-47
lines changed

src/Debug/debugger.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
227227
this->handleUpdateModule(m, interruptData);
228228
free(interruptData);
229229
break;
230+
case interruptINVOKE:
231+
this->handleInvoke(m, interruptData);
232+
free(interruptData);
233+
break;
230234
case interruptWOODDUMP:
231235
*program_state = WARDUINOpause;
232236
free(interruptData);
@@ -350,6 +354,15 @@ uint8_t *Debugger::findOpcode(Module *m, Block *block) {
350354
return opcode;
351355
}
352356

357+
void Debugger::handleInvoke(Module *m, uint8_t *interruptData) {
358+
uint32_t fidx = read_L32(&interruptData);
359+
360+
Type func = *m->functions[fidx].type;
361+
StackValue *args = readArgs(func, interruptData);
362+
363+
WARDuino::instance()->invoke(m, fidx, func.param_count, args);
364+
}
365+
353366
void Debugger::handleInterruptRUN(Module *m, RunningState *program_state) {
354367
this->channel->write("GO!\n");
355368
if (*program_state == WARDUINOpause && this->isBreakpoint(m->pc_ptr)) {

src/Debug/debugger.h

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ enum InterruptTypes {
4646
interruptUPDATELocal = 0x21,
4747
interruptUPDATEModule = 0x22,
4848

49+
// Remote REPL
50+
interruptINVOKE = 0x40,
51+
4952
// Pull Debugging
5053
interruptWOODDUMP = 0x60,
5154
interruptOffset = 0x61,
@@ -94,6 +97,10 @@ class Debugger {
9497

9598
void pushMessage(uint8_t *msg);
9699

100+
//// Handle REPL interrupts
101+
102+
void handleInvoke(Module *m, uint8_t *interruptData);
103+
97104
//// Handle Interrupt Types
98105

99106
void handleInterruptRUN(Module *m, RunningState *program_state);

src/Edward/proxy.cpp

+1-47
Original file line numberDiff line numberDiff line change
@@ -99,53 +99,7 @@ char *printValue(StackValue *v) {
9999
}
100100

101101
StackValue *Proxy::readRFCArgs(Block *func, uint8_t *data) {
102-
if (func->type->param_count == 0) {
103-
printf("ProxyFunc %" PRIu32 "takes no arg\n", func->fidx);
104-
return nullptr;
105-
}
106-
107-
auto *args = new StackValue[func->type->param_count];
108-
uint32_t *params = func->type->params;
109-
for (uint32_t i = 0; i < func->type->param_count; i++) {
110-
args[i].value.uint64 = 0; // init whole union to 0
111-
args[i].value_type = params[i];
112-
113-
switch (params[i]) {
114-
case I32: {
115-
memcpy(&args[i].value.uint32, data, sizeof(uint32_t));
116-
data += sizeof(uint32_t);
117-
printf("arg %" PRIu32 ": i32 value %" PRIu32 "\n", i,
118-
args[i].value.uint32);
119-
break;
120-
}
121-
case F32: {
122-
memcpy(&args[i].value.f32, data, sizeof(float));
123-
data += sizeof(float);
124-
printf("arg %" PRIu32 ": F32 value %.7f \n", i,
125-
args[i].value.f32);
126-
break;
127-
}
128-
case I64: {
129-
memcpy(&args[i].value.uint64, data, sizeof(uint64_t));
130-
data += sizeof(uint64_t);
131-
printf("arg %" PRIu32 ": I64 value %" PRIu64 "\n", i,
132-
args[i].value.uint64);
133-
break;
134-
}
135-
case F64: {
136-
memcpy(&args[i].value.f64, data, sizeof(double));
137-
data += sizeof(double);
138-
printf("arg %" PRIu32 ": f64 value %.7f \n", i,
139-
args[i].value.f64);
140-
break;
141-
}
142-
default: {
143-
FATAL("incorrect argument type: %" PRIu32 "\n", params[i]);
144-
break;
145-
}
146-
}
147-
}
148-
return args;
102+
return readArgs(*func->type, data);
149103
}
150104

151105
void Proxy::setupCalleeArgs(Module *m, RFC *callee) {

src/Utils/util.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55

66
#include "macros.h"
77

8+
StackValue *readArgs(Type function, uint8_t *data) {
9+
auto *args = new StackValue[function.param_count];
10+
for (uint32_t i = 0; i < function.param_count; i++) {
11+
args[i] = {static_cast<uint8_t>(function.params[i]), {0}};
12+
13+
switch (args[i].value_type) {
14+
case I32: {
15+
memcpy(&args[i].value.uint32, data, sizeof(uint32_t));
16+
data += sizeof(uint32_t);
17+
break;
18+
}
19+
case F32: {
20+
memcpy(&args[i].value.f32, data, sizeof(float));
21+
data += sizeof(float);
22+
break;
23+
}
24+
case I64: {
25+
memcpy(&args[i].value.uint64, data, sizeof(uint64_t));
26+
data += sizeof(uint64_t);
27+
break;
28+
}
29+
case F64: {
30+
memcpy(&args[i].value.f64, data, sizeof(double));
31+
data += sizeof(double);
32+
break;
33+
}
34+
default: {
35+
FATAL("no argument of type %" PRIu32 "\n", args[i].value_type);
36+
}
37+
}
38+
}
39+
return args;
40+
}
41+
842
// Little endian base (LED128)
943

1044
uint64_t read_LEB_(uint8_t **pos, uint32_t maxbits, bool sign) {

src/Utils/util.h

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
#include <cstdlib>
77
#include <string>
88

9+
#include "../WARDuino.h"
10+
11+
/**
12+
* Read arguments for function from binary data.
13+
*
14+
* @param type
15+
* @param data
16+
* @return The arguments as a list of StackValues. Returns nullptr for arity 0.
17+
*/
18+
StackValue *readArgs(Type function, uint8_t *data);
19+
920
/**
1021
* Read a Little endian base value of 32 bits
1122
* see: https://en.wikipedia.org/wiki/LEB128

0 commit comments

Comments
 (0)