~sircmpwn/ipcgen

ref: 2cdc53095a052b4f5ce3fdc6e410f2dd17eea54d ipcgen/gen/server.ha -rw-r--r-- 6.1 KiB
2cdc5309Drew DeVault client: implement return values 1 year, 19 days ago
                                                                                
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use ast;
use ast::{ptype};
use fmt;
use io;
use strio;
use tmpl = strings::template;

// Generates code for a server to implement the given interface.
export fn server(out: io::handle, doc: *ast::document) (void | io::error) = {
	fmt::fprintln(out, "// This file was generated by ipcgen; do not modify by hand")!;
	fmt::fprintln(out, "use errors;")!;
	fmt::fprintln(out, "use helios;")!;
	fmt::fprintln(out, "use rt;")!;
	fmt::fprintln(out)!;

	for (let i = 0z; i < len(doc.interfaces); i += 1) {
		const iface = &doc.interfaces[i];
		s_iface(out, doc, iface)?;
	};
};

const s_iface_header_src: str =
	`export type $iface_iface = struct {`;
let st_iface_header: tmpl::template = [];

const s_iface_method_src: str =
	`	$method: *fn_$iface_$method,`;
let st_iface_method: tmpl::template = [];

@init fn s_iface() void = {
	st_iface_header = tmpl::compile(s_iface_header_src)!;
	st_iface_method = tmpl::compile(s_iface_method_src)!;
};

fn s_iface(
	out: io::handle,
	doc: *ast::document,
	iface: *ast::interface,
) (void | io::error) = {
	const id: ast::ident = [iface.name];
	const name = gen_name_upper(&id);
	defer free(name);

	let id: ast::ident = alloc(doc.namespace...);
	append(id, iface.name);
	defer free(id);
	const hash = genhash(&id);

	fmt::fprintfln(out, "def {}_ID: u32 = 0x{:X};\n", name, hash)!;

	for (let i = 0z; i < len(iface.methods); i += 1) {
		const meth = &iface.methods[i];
		s_method_fntype(out, iface, meth)?;
	};
	fmt::fprintln(out)?;

	tmpl::execute(&st_iface_header, out,
		("iface", iface.name),
	)?;
	fmt::fprintln(out)?;

	for (let i = 0z; i < len(iface.methods); i += 1) {
		const meth = &iface.methods[i];
		tmpl::execute(&st_iface_method, out,
			("iface", iface.name),
			("method", meth.name),
		)?;
		fmt::fprintln(out)?;
	};

	fmt::fprintln(out, "};\n")?;

	iface_label(out, iface, name)?;
	fmt::fprintln(out)!;

	s_iface_object(out, iface)?;
	fmt::fprintln(out)!;

	s_iface_dispatch(out, iface)?;
	fmt::fprintln(out)!;
};

const s_method_fntype_src: str =
	`export type fn_$iface_$method = fn(object: *$object$params) $result;`;
let st_method_fntype: tmpl::template = [];

@init fn s_method_fntype() void = {
	st_method_fntype= tmpl::compile(s_method_fntype_src)!;
};

fn s_method_fntype(
	out: io::handle,
	iface: *ast::interface,
	meth: *ast::method,
) (void | io::error) = {
	assert(len(meth.caps_in) == 0); // TODO
	assert(len(meth.caps_out) == 0); // TODO

	let params = strio::dynamic();
	defer io::close(&params)!;
	if (len(meth.params) != 0) {
		fmt::fprint(&params, ", ")?;
	};
	for (let i = 0z; i < len(meth.params); i += 1) {
		const param = &meth.params[i];
		fmt::fprintf(&params, "{}: ", param.name)!;
		ipc_type(&params, &param.param_type)!;

		if (i + 1 < len(meth.params)) {
			fmt::fprint(&params, ", ")!;
		};
	};

	let result = strio::dynamic();
	defer io::close(&result)!;
	ipc_type(&result, &meth.result)!;

	tmpl::execute(&st_method_fntype, out,
		("method", meth.name),
		("iface", iface.name),
		("object", iface.name),
		("params", strio::string(&params)),
		("result", strio::string(&result)),
	)?;
	fmt::fprintln(out)?;
};

const s_iface_object_src: str = `export type $iface = struct {
	_iface: *$iface_iface,
	_endpoint: helios::cap,
};`;
let st_iface_object: tmpl::template = [];

@init fn s_iface_object() void = {
	st_iface_object = tmpl::compile(s_iface_object_src)!;
};

fn s_iface_object(
	out: io::handle,
	iface: *ast::interface,
) (void | io::error) = {
	tmpl::execute(&st_iface_object, out,
		("iface", iface.name),
	)?;
	fmt::fprintln(out)!;
};

const s_iface_dispatch_header_src: str = `export fn $iface_dispatch(
	object: *$iface,
) void = {
	const (tag, a1) = helios::recvraw(object._endpoint);
	switch (rt::label(tag): $iface_label) {
`;

const s_iface_dispatch_footer_src: str = `	case =>
		abort(); // TODO
	};
};
`;

// XXX: It might be nice if we didn't have to do a reply syscall to detect that
// the user stored the reply capability for later
const s_iface_dispatch_method_src: str = `	case $iface_label::$methodid =>
		${rval_store}object._iface.$method(
			object,$params
		);
		match (helios::reply(0${rval_param})) {
		case void =>
			yield;
		case errors::invalid_cslot =>
			yield; // callee stored the reply
		case errors::error =>
			abort(); // TODO
		};
`;
let st_iface_dispatch_header: tmpl::template = [];
let st_iface_dispatch_footer: tmpl::template = [];
let st_iface_dispatch_method: tmpl::template = [];

@init fn s_iface_dispatch() void = {
	st_iface_dispatch_header = tmpl::compile(s_iface_dispatch_header_src)!;
	st_iface_dispatch_footer = tmpl::compile(s_iface_dispatch_footer_src)!;
	st_iface_dispatch_method = tmpl::compile(s_iface_dispatch_method_src)!;
};

fn s_iface_dispatch(
	out: io::handle,
	iface: *ast::interface,
) (void | io::error) = {
	tmpl::execute(&st_iface_dispatch_header, out,
		("iface", iface.name),
	)?;

	for (let serial = 0z; serial < len(iface.methods); serial += 1) {
		const meth = &iface.methods[serial];
		s_method_dispatch(out, iface, meth)?;
	};

	tmpl::execute(&st_iface_dispatch_footer, out)?;
};

fn s_method_dispatch(
	out: io::handle,
	iface: *ast::interface,
	meth: *ast::method,
) (void | io::error) = {
	const id: ast::ident = [meth.name];
	const method_id = gen_name_upper(&id);
	defer free(method_id);

	let params = strio::dynamic();
	defer io::close(&params)!;
	for (let i = 0z; i < len(meth.params); i += 1) {
		fmt::fprint(&params, "\n\t\t\t")!;
		const param = &meth.params[i];
		if (i == 0) {
			fmt::fprint(&params, "a1")!;
		} else {
			fmt::fprintf(&params, "rt::ipcbuf.params[{}]", i)!;
		};

		fmt::fprint(&params, ": ")!;
		ipc_type(&params, &param.param_type)!;
		fmt::fprint(&params, ",")!;
	};

	let rval_store = strio::dynamic();
	defer io::close(&rval_store)!;
	let rval_param = strio::dynamic();
	defer io::close(&rval_param)!;
	if (!(meth.result is ptype) || meth.result as ptype != ptype::VOID) {
		fmt::fprintf(&rval_store, "const rval = ")!;
		fmt::fprintf(&rval_param, ", rval")!;
	};

	tmpl::execute(&st_iface_dispatch_method, out,
		("iface", iface.name),
		("method", meth.name),
		("methodid", method_id),
		("params", strio::string(&params)),
		("rval_store", strio::string(&rval_store)),
		("rval_param", strio::string(&rval_param)),
	)?;
};