diff options
| author | 2026-10-12 08:46:23 +0330 | |
|---|---|---|
| committer | 2026-10-12 08:46:23 +0330 | |
| commit | ecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch) | |
| tree | 70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /src/micro/port_wasm_stream.c | |
a
Diffstat (limited to 'src/micro/port_wasm_stream.c')
| -rw-r--r-- | src/micro/port_wasm_stream.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/micro/port_wasm_stream.c b/src/micro/port_wasm_stream.c new file mode 100644 index 0000000..155cde4 --- /dev/null +++ b/src/micro/port_wasm_stream.c @@ -0,0 +1,121 @@ +/* WebAssembly port of the microcore with streaming input, for the language + server. the host (the vs code extension) feeds the server's input in pieces + and calls wasm_resume after each feed; the server's output comes back one + byte at a time through js_putc, and its diagnostics through js_msg. the + server suspends when it wants input it does not have yet (MICRO_WAIT in + micro_port.h), so a step never blocks the host. build with + src/tools/mklspwasm.sh; nothing here needs libc. */ +#include "micro_port.h" + +extern void js_putc(int c); +extern void js_msg(int c); + +#define CHUNK 65536 +#define QCAP (8 << 20) + +/* the host copies a piece of input here, then calls wasm_feed with its length */ +static unsigned char chunk[CHUNK]; +/* the input the server has not read yet: q[qp..qn) */ +static unsigned char q[QCAP]; +static int qn, qp, closed; + +unsigned char *wasm_chunk(void) { return chunk; } +int wasm_chunk_cap(void) { return (int)sizeof chunk; } + +/* appends the first n bytes of chunk to the input. 0 on success, -1 when the + input would not fit, which the host must not allow (it drains by running) */ +int wasm_feed(int n) +{ + int i; + if (n < 0 || n > CHUNK || closed) return -1; + if (qn + n > QCAP) { /* move what is unread to the front */ + int live = qn - qp; + if (live + n > QCAP) return -1; + for (i = 0; i < live; i++) q[i] = q[qp + i]; + qp = 0; qn = live; + } + for (i = 0; i < n; i++) q[qn++] = chunk[i]; + return 0; +} + +/* no more input will come: the next read past the end is -1 */ +void wasm_close(void) { closed = 1; } + +/* the machine's memory: the linear memory above everything the module has in it. + the base is fixed when the first machine is made, and a grow only adds pages on + the top, so the base never moves. */ +static unsigned char *mbase; +static UCell mcap; /* bytes of machine memory the module has grown for */ + +Cell *micro_mem_alloc(UCell words) +{ + UCell need, extra, pages; + if (words > ((UCell)-1) / sizeof(Cell)) return 0; + if (!mbase) { + UCell top = (UCell)__builtin_wasm_memory_size(0) * 65536u; + mbase = (unsigned char *)top; + mcap = 0; + } + need = words * sizeof(Cell); + if (need <= mcap) return (Cell *)mbase; + extra = need - mcap; + pages = (extra + 65535u) / 65536u; + if (__builtin_wasm_memory_grow(0, (int)pages) < 0) return 0; + mcap += pages * 65536u; /* wasm zeroes the pages it grows by */ + return (Cell *)mbase; +} + +int micro_getc(void) +{ + if (qp < qn) return q[qp++]; + return closed ? -1 : MICRO_WAIT; +} +int micro_eof(void) { return closed && qp >= qn; } +void micro_putc(int c) { js_putc(c & 255); } +void micro_msg(const char *s) { while (*s) js_msg(*s++); } + +Cell micro_syscall(Cell *mem, Cell op, Cell a, Cell b, Cell c) +{ + switch (op) { + case 9: /* memory: the size of the machine and its heap */ + if (a == 0) return micro_mem_words(); + if (a == 1) return HEAP_LO; + if (a == 2) return HEAP_HI; + if (a == 3) return (Cell)micro_mem_words() * 8; + return -1; + case 7: /* exit: the run ends */ + return -1; + case 21: /* argc: the server has no command line of its own, so <eval> alone */ + return 1; + case 22: /* argv: entry 0 is <eval>, as in zy(1) */ + if (a != 0) return -1; + if (!b) return 6; + if (c < 7 || b + 6 >= micro_mem_words()) return -1; + { + static const char lead[] = "<eval>"; + int i; + for (i = 0; i < 6; i++) mem[b + i] = (unsigned char)lead[i]; + mem[b + 6] = 0; + } + return 6; + case 23: /* envc: the host passes no environment */ + return 0; + } + return -1; +} + +/* the core multiplies 64 by 64 into 128 bits for the float helpers; wasm has no + such instruction and there is no compiler runtime to link, so this is it */ +void __multi3(unsigned long long *r, unsigned long long al, unsigned long long ah, unsigned long long bl, unsigned long long bh) +{ + unsigned long long a0 = al & 0xffffffffu, a1 = al >> 32, b0 = bl & 0xffffffffu, b1 = bl >> 32; + unsigned long long p00 = a0 * b0, p01 = a0 * b1, p10 = a1 * b0, p11 = a1 * b1; + unsigned long long mid = (p00 >> 32) + (p01 & 0xffffffffu) + (p10 & 0xffffffffu); + r[0] = (p00 & 0xffffffffu) | (mid << 32); + r[1] = p11 + (p01 >> 32) + (p10 >> 32) + (mid >> 32) + al * bh + ah * bl; +} + +/* loads the server and runs it until it halts or waits for input (3) */ +int wasm_start(void) { return micro_run(); } +/* goes on after the host fed input: as wasm_start, halt (0), error (2) or wait (3) */ +int wasm_resume(void) { return micro_resume(); } |
