aboutsummaryrefslogtreecommitdiff
path: root/src/micro/port_wasm_stream.c
blob: d96ceab500c9caf4200718e69890176af8192357 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* 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, polled;

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;
}

/* 1 while the server is suspended by a poll rather than by a read: the host should resume it
   after the event loop has run, whether or not input came */
int wasm_polling(void) { return polled; }

/* 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 42:	/* poll: suspend once so the host can feed what has arrived, then say if input is waiting */
		if (!polled) { polled = 1; return MICRO_WAIT; }
		polled = 0;
		return qp < qn;
	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(); }