vile / scheme / port_custom.v
801 lines · 782 sloc · 28.15 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// Scheme resources are deliberately separate from synchronous host Readers and
6// Writers. Their callbacks must return to the running Machine's bridge.
7@[heap]
8struct CustomPortResource {
9 id string
10 read Value
11 write Value
12 get_position Value
13 set_position Value
14 close Value
15mut:
16 text_view Value
17 view_start int
18 view_count int
19 view_revision u64
20 replay_active bool
21 replay_tail []u8
22 cursor_lease &CustomPortLease = unsafe { nil }
23 cursor_epoch u64
24 read_buffer Value
25 closed bool
26 active &CustomPortLease = unsafe { nil }
27 active_context u64
28 input_adapter &CustomTranscodedInput = unsafe { nil }
29 output_adapter &CustomTranscodedOutput = unsafe { nil }
30 duplex &InputOutputPort = unsafe { nil }
31 close_attempted bool
32 io_lease &CustomPortLease = unsafe { nil }
33 io_epoch u64
34 binary_position Value
35 text_cursor &CustomTextCursor = unsafe { nil }
36}
37
38struct CustomPortOutput {
39 resource &CustomPortResource
40}
41
42fn (_ &CustomPortOutput) write(_bytes []u8) !int {
43 return failure('Scheme port operation needs the resumable I/O driver')
44}
45
46enum PortIoOperation {
47 write_u8
48 put_u8
49 write_bytes
50 put_bytes
51 close_output
52 close_any
53 get_position
54 set_position
55 read_u8
56 peek_u8
57 get_u8
58 lookahead_u8
59 read_bytes
60 get_bytes
61 read_into
62 get_into
63 read_some
64 read_all
65 input_eof
66 close_input
67 read_char
68 peek_char
69 get_char
70 lookahead_char
71 read_text
72 get_text
73 text_all
74 read_line
75 get_line
76 text_into
77 write_char
78 put_char
79 write_text
80 put_text
81 display_text
82 write_datum
83 write_shared
84 write_simple
85 put_datum
86 newline
87 read_datum
88 get_datum
89 flush_output
90}
91
92const custom_binary_output_name = 'make-custom-binary-output-port'
93const custom_port_names = 'make-custom-binary-input-port make-custom-binary-output-port make-custom-binary-input/output-port make-custom-textual-input-port make-custom-textual-output-port make-custom-textual-input/output-port'
94
95fn (mut vm VM) install_custom_port_constructors() {
96 vm.install_custom_text_constructors()
97 vm.bind_global(custom_binary_input_name, procedure_value(&Procedure{ mode: .native, name: custom_binary_input_name, owner: vm.owner, native: custom_binary_input }))
98 vm.bind_global(custom_binary_output_name, procedure_value(&Procedure{
99 mode: .native
100 name: custom_binary_output_name
101 owner: vm.owner
102 native: custom_binary_output
103 }))
104 vm.install_custom_combined_constructors()
105}
106
107fn (vm &VM) check_custom_port_callback(value Value, optional bool) ! {
108 if optional && value.tag == .boolean && !value.is_true() { return }
109 if !value.is_callable() {
110 return failure('custom port callback must be a procedure or optional #f')
111 }
112 vm.check_callable_owner(value)!
113}
114
115fn custom_binary_output(mut vm VM, args []Value) !Value {
116 check_arity(custom_binary_output_name, args.len, 5, 5)!
117 return vm.make_custom_binary_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4])
118}
119
120pub fn (mut vm VM) make_custom_binary_output_port(id string, write_callback Value, get_position Value, set_position Value, close Value) !Value {
121 vm.ready()!
122 vm.check_custom_port_callback(write_callback, false)!
123 vm.check_custom_port_callback(get_position, true)!
124 vm.check_custom_port_callback(set_position, true)!
125 vm.check_custom_port_callback(close, true)!
126 resource := &CustomPortResource{ id: id, write: write_callback, get_position: get_position, set_position: set_position, close: close }
127 return vm.output_port(&CustomPortOutput{ resource: resource }, binary: true)
128}
129
130fn custom_output_resource(value Value) &CustomPortResource {
131 if value.tag in [.output_port, .input_output_port] {
132 port := value.output_pointer()
133 if port.sink is CustomPortOutput { return port.sink.resource }
134 if port.sink is CustomTranscodedOutput { return port.sink.resource }
135 }
136 return unsafe { nil }
137}
138
139// Only I/O bindings use this mode; ordinary native calls incur no new branch.
140// Its function is also the real continuation bridge, with the operation code in
141// the entry instruction. Every instruction table has process lifetime.
142fn (vm &VM) port_native_procedure(entry &NativeDescriptor) Value {
143 index := match entry.name {
144 'write-u8' { 0 }
145 'put-u8' { 1 }
146 'write-bytevector' { 2 }
147 'put-bytevector' { 3 }
148 'close-output-port' { 4 }
149 'close-port' { 5 }
150 'port-position' { 6 }
151 'set-port-position!' { 7 }
152 'read-u8' { 8 }
153 'peek-u8' { 9 }
154 'get-u8' { 10 }
155 'lookahead-u8' { 11 }
156 'read-bytevector' { 12 }
157 'get-bytevector-n' { 13 }
158 'read-bytevector!' { 14 }
159 'get-bytevector-n!' { 15 }
160 'get-bytevector-some' { 16 }
161 'get-bytevector-all' { 17 }
162 'port-eof?' { 18 }
163 'close-input-port' { 19 }
164 'read-char' { 20 }
165 'peek-char' { 21 }
166 'get-char' { 22 }
167 'lookahead-char' { 23 }
168 'read-string' { 24 }
169 'get-string-n' { 25 }
170 'get-string-all' { 26 }
171 'read-line' { 27 }
172 'get-line' { 28 }
173 'get-string-n!' { 29 }
174 'write-char' { 30 }
175 'put-char' { 31 }
176 'write-string' { 32 }
177 'put-string' { 33 }
178 'display' { 34 }
179 'write' { 35 }
180 'write-shared' { 36 }
181 'write-simple' { 37 }
182 'put-datum' { 38 }
183 'newline' { 39 }
184 'read' { 40 }
185 'get-datum' { 41 }
186 'flush-output-port' { 42 }
187 else { -1 }
188 }
189 return procedure_value(&Procedure{
190 mode: if index < 0 { ProcedureKind.native } else { ProcedureKind.port_io }
191 name: entry.name
192 owner: vm.owner
193 native: entry.callback
194 function: if index < 0 { unsafe { nil } } else { &custom_port_io_bridges[index] }
195 })
196}
197
198// Probe only an existing argument/parameter location. Ordinary calls keep their
199// native validation order, and omitted stdio is not materialized by this probe.
200fn (m &Machine) custom_port_target(operation PortIoOperation, args []Value, vm &VM) Value {
201 index := if operation in [.write_u8, .write_bytes, .read_bytes, .read_into, .read_text,
202 .write_char, .write_text, .display_text, .write_datum, .write_shared, .write_simple] {
203 1
204 } else {
205 0
206 }
207 default_input := operation in [.read_u8, .peek_u8, .read_bytes, .read_into, .read_char, .peek_char,
208 .read_text, .read_line, .read_datum]
209 target := if index < args.len {
210 args[index]
211 } else if (index == 1 && args.len == 1) || ((default_input || operation in [
212 .newline,
213 .flush_output,
214 ]) && index == 0 && args.len == 0) {
215 m.parameter_value(vm.port_parameters[if default_input { 0 } else { 1 }].procedure_pointer())
216 } else {
217 return unspecified()
218 }
219 probe := custom_combined_facet(operation, target)
220 return if isnil(custom_port_resource(probe)) { unspecified() } else { target }
221}
222
223const port_io_names = ['write-u8', 'put-u8', 'write-bytevector', 'put-bytevector', 'close-output-port',
224 'close-port', 'port-position', 'set-port-position!', 'read-u8', 'peek-u8', 'get-u8', 'lookahead-u8',
225 'read-bytevector', 'get-bytevector-n', 'read-bytevector!', 'get-bytevector-n!',
226 'get-bytevector-some', 'get-bytevector-all', 'port-eof?', 'close-input-port', 'read-char',
227 'peek-char', 'get-char', 'lookahead-char', 'read-string', 'get-string-n', 'get-string-all',
228 'read-line', 'get-line', 'get-string-n!', 'write-char', 'put-char', 'write-string', 'put-string',
229 'display', 'write', 'write-shared', 'write-simple', 'put-datum', 'newline', 'read', 'get-datum',
230 'flush-output-port']!
231
232fn (mut vm VM) prepare_custom_port_operation(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation {
233 check_custom_port_arity(operation, args.len)!
234 if target.tag == .input_output_port {
235 return vm.prepare_custom_combined_operation(operation, target, args)
236 }
237 if target.tag == .input_port {
238 return vm.prepare_custom_input_operation(operation, target, args)
239 }
240 if custom_input_only(operation) { return failure('expected an input port') }
241 if target.tag == .unspecified { return failure('expected a custom port') }
242 if !target.output_pointer().binary {
243 return vm.prepare_custom_text_output(operation, target, args)
244 }
245 closing := operation in [.close_output, .close_any]
246 port := if closing { vm.output_owner(target)! } else { vm.checked_output(target)! }
247 if port.busy { return failure('output port operation is already active') }
248 resource := custom_output_resource(target)
249 mut source := unspecified()
250 mut start := 0
251 mut end := 0
252 mut position := unspecified()
253 match operation {
254 .write_u8, .put_u8 {
255 byte := byte_value(args[if operation == .write_u8 { 0 } else { 1 }])!
256 source = owned_bytevector([byte])
257 end = 1
258 }
259 .write_bytes, .put_bytes {
260 source = args[if operation == .write_bytes { 0 } else { 1 }]
261 data := bytevector_data(source)!
262 start, end = if operation == .write_bytes {
263 sequence_range(args, 2, data.bytes.len)!
264 } else {
265 port_get_put_range(args, data.bytes.len)!
266 }
267 }
268 .get_position {
269 if !resource.get_position.is_callable() {
270 return failure('output port does not support positions')
271 }
272 }
273 .set_position {
274 position = args[1]
275 check_custom_binary_position(position)!
276 if !resource.set_position.is_callable() {
277 return failure('output port does not support setting positions')
278 }
279 }
280 .close_output, .close_any, .flush_output {}
281 else { return failure('expected a textual output port') }
282 }
283 if operation in [.write_u8, .put_u8, .write_bytes, .put_bytes] && start == end {
284 return unsafe { nil }
285 }
286 return &CustomPortOperation{
287 operation: operation
288 target: target
289 resource: resource
290 source: source
291 end: end
292 position: position
293 offset: start
294 lease: &CustomPortLease{ port: target, resource: resource, closing: closing }
295 }
296}
297
298fn check_custom_binary_position(value Value) ! {
299 if !is_exact_integer(value) || exact_sign(value) < 0 {
300 return failure('custom binary port position must be a nonnegative exact integer')
301 }
302}
303
304const custom_port_io_code = [
305 // PortIoOperation 0
306 Instruction{ op: .port_io_enter, a: 0, origin: reader.no_node }
307 Instruction{ op: .port_io_step, origin: reader.no_node }
308 Instruction{ op: .port_io_collect, origin: reader.no_node }
309 Instruction{ op: .jump, a: 1, origin: reader.no_node }
310 // PortIoOperation 1
311 Instruction{ op: .port_io_enter, a: 1, origin: reader.no_node }
312 Instruction{ op: .port_io_step, origin: reader.no_node }
313 Instruction{ op: .port_io_collect, origin: reader.no_node }
314 Instruction{ op: .jump, a: 1, origin: reader.no_node }
315 // PortIoOperation 2
316 Instruction{ op: .port_io_enter, a: 2, origin: reader.no_node }
317 Instruction{ op: .port_io_step, origin: reader.no_node }
318 Instruction{ op: .port_io_collect, origin: reader.no_node }
319 Instruction{ op: .jump, a: 1, origin: reader.no_node }
320 // PortIoOperation 3
321 Instruction{ op: .port_io_enter, a: 3, origin: reader.no_node }
322 Instruction{ op: .port_io_step, origin: reader.no_node }
323 Instruction{ op: .port_io_collect, origin: reader.no_node }
324 Instruction{ op: .jump, a: 1, origin: reader.no_node }
325 // PortIoOperation 4
326 Instruction{ op: .port_io_enter, a: 4, origin: reader.no_node }
327 Instruction{ op: .port_io_step, origin: reader.no_node }
328 Instruction{ op: .port_io_collect, origin: reader.no_node }
329 Instruction{ op: .jump, a: 1, origin: reader.no_node }
330 // PortIoOperation 5
331 Instruction{ op: .port_io_enter, a: 5, origin: reader.no_node }
332 Instruction{ op: .port_io_step, origin: reader.no_node }
333 Instruction{ op: .port_io_collect, origin: reader.no_node }
334 Instruction{ op: .jump, a: 1, origin: reader.no_node }
335 // PortIoOperation 6
336 Instruction{ op: .port_io_enter, a: 6, origin: reader.no_node }
337 Instruction{ op: .port_io_step, origin: reader.no_node }
338 Instruction{ op: .port_io_collect, origin: reader.no_node }
339 Instruction{ op: .jump, a: 1, origin: reader.no_node }
340 // PortIoOperation 7
341 Instruction{ op: .port_io_enter, a: 7, origin: reader.no_node }
342 Instruction{ op: .port_io_step, origin: reader.no_node }
343 Instruction{ op: .port_io_collect, origin: reader.no_node }
344 Instruction{ op: .jump, a: 1, origin: reader.no_node }
345 // PortIoOperation 8
346 Instruction{ op: .port_io_enter, a: 8, origin: reader.no_node }
347 Instruction{ op: .port_io_step, origin: reader.no_node }
348 Instruction{ op: .port_io_collect, origin: reader.no_node }
349 Instruction{ op: .jump, a: 1, origin: reader.no_node }
350 // PortIoOperation 9
351 Instruction{ op: .port_io_enter, a: 9, origin: reader.no_node }
352 Instruction{ op: .port_io_step, origin: reader.no_node }
353 Instruction{ op: .port_io_collect, origin: reader.no_node }
354 Instruction{ op: .jump, a: 1, origin: reader.no_node }
355 // PortIoOperation 10
356 Instruction{ op: .port_io_enter, a: 10, origin: reader.no_node }
357 Instruction{ op: .port_io_step, origin: reader.no_node }
358 Instruction{ op: .port_io_collect, origin: reader.no_node }
359 Instruction{ op: .jump, a: 1, origin: reader.no_node }
360 // PortIoOperation 11
361 Instruction{ op: .port_io_enter, a: 11, origin: reader.no_node }
362 Instruction{ op: .port_io_step, origin: reader.no_node }
363 Instruction{ op: .port_io_collect, origin: reader.no_node }
364 Instruction{ op: .jump, a: 1, origin: reader.no_node }
365 // PortIoOperation 12
366 Instruction{ op: .port_io_enter, a: 12, origin: reader.no_node }
367 Instruction{ op: .port_io_step, origin: reader.no_node }
368 Instruction{ op: .port_io_collect, origin: reader.no_node }
369 Instruction{ op: .jump, a: 1, origin: reader.no_node }
370 // PortIoOperation 13
371 Instruction{ op: .port_io_enter, a: 13, origin: reader.no_node }
372 Instruction{ op: .port_io_step, origin: reader.no_node }
373 Instruction{ op: .port_io_collect, origin: reader.no_node }
374 Instruction{ op: .jump, a: 1, origin: reader.no_node }
375 // PortIoOperation 14
376 Instruction{ op: .port_io_enter, a: 14, origin: reader.no_node }
377 Instruction{ op: .port_io_step, origin: reader.no_node }
378 Instruction{ op: .port_io_collect, origin: reader.no_node }
379 Instruction{ op: .jump, a: 1, origin: reader.no_node }
380 // PortIoOperation 15
381 Instruction{ op: .port_io_enter, a: 15, origin: reader.no_node }
382 Instruction{ op: .port_io_step, origin: reader.no_node }
383 Instruction{ op: .port_io_collect, origin: reader.no_node }
384 Instruction{ op: .jump, a: 1, origin: reader.no_node }
385 // PortIoOperation 16
386 Instruction{ op: .port_io_enter, a: 16, origin: reader.no_node }
387 Instruction{ op: .port_io_step, origin: reader.no_node }
388 Instruction{ op: .port_io_collect, origin: reader.no_node }
389 Instruction{ op: .jump, a: 1, origin: reader.no_node }
390 // PortIoOperation 17
391 Instruction{ op: .port_io_enter, a: 17, origin: reader.no_node }
392 Instruction{ op: .port_io_step, origin: reader.no_node }
393 Instruction{ op: .port_io_collect, origin: reader.no_node }
394 Instruction{ op: .jump, a: 1, origin: reader.no_node }
395 // PortIoOperation 18
396 Instruction{ op: .port_io_enter, a: 18, origin: reader.no_node }
397 Instruction{ op: .port_io_step, origin: reader.no_node }
398 Instruction{ op: .port_io_collect, origin: reader.no_node }
399 Instruction{ op: .jump, a: 1, origin: reader.no_node }
400 // PortIoOperation 19
401 Instruction{ op: .port_io_enter, a: 19, origin: reader.no_node }
402 Instruction{ op: .port_io_step, origin: reader.no_node }
403 Instruction{ op: .port_io_collect, origin: reader.no_node }
404 Instruction{ op: .jump, a: 1, origin: reader.no_node }
405 // PortIoOperation 20
406 Instruction{ op: .port_io_enter, a: 20, origin: reader.no_node }
407 Instruction{ op: .port_io_step, origin: reader.no_node }
408 Instruction{ op: .port_io_collect, origin: reader.no_node }
409 Instruction{ op: .jump, a: 1, origin: reader.no_node }
410 // PortIoOperation 21
411 Instruction{ op: .port_io_enter, a: 21, origin: reader.no_node }
412 Instruction{ op: .port_io_step, origin: reader.no_node }
413 Instruction{ op: .port_io_collect, origin: reader.no_node }
414 Instruction{ op: .jump, a: 1, origin: reader.no_node }
415 // PortIoOperation 22
416 Instruction{ op: .port_io_enter, a: 22, origin: reader.no_node }
417 Instruction{ op: .port_io_step, origin: reader.no_node }
418 Instruction{ op: .port_io_collect, origin: reader.no_node }
419 Instruction{ op: .jump, a: 1, origin: reader.no_node }
420 // PortIoOperation 23
421 Instruction{ op: .port_io_enter, a: 23, origin: reader.no_node }
422 Instruction{ op: .port_io_step, origin: reader.no_node }
423 Instruction{ op: .port_io_collect, origin: reader.no_node }
424 Instruction{ op: .jump, a: 1, origin: reader.no_node }
425 // PortIoOperation 24
426 Instruction{ op: .port_io_enter, a: 24, origin: reader.no_node }
427 Instruction{ op: .port_io_step, origin: reader.no_node }
428 Instruction{ op: .port_io_collect, origin: reader.no_node }
429 Instruction{ op: .jump, a: 1, origin: reader.no_node }
430 // PortIoOperation 25
431 Instruction{ op: .port_io_enter, a: 25, origin: reader.no_node }
432 Instruction{ op: .port_io_step, origin: reader.no_node }
433 Instruction{ op: .port_io_collect, origin: reader.no_node }
434 Instruction{ op: .jump, a: 1, origin: reader.no_node }
435 // PortIoOperation 26
436 Instruction{ op: .port_io_enter, a: 26, origin: reader.no_node }
437 Instruction{ op: .port_io_step, origin: reader.no_node }
438 Instruction{ op: .port_io_collect, origin: reader.no_node }
439 Instruction{ op: .jump, a: 1, origin: reader.no_node }
440 // PortIoOperation 27
441 Instruction{ op: .port_io_enter, a: 27, origin: reader.no_node }
442 Instruction{ op: .port_io_step, origin: reader.no_node }
443 Instruction{ op: .port_io_collect, origin: reader.no_node }
444 Instruction{ op: .jump, a: 1, origin: reader.no_node }
445 // PortIoOperation 28
446 Instruction{ op: .port_io_enter, a: 28, origin: reader.no_node }
447 Instruction{ op: .port_io_step, origin: reader.no_node }
448 Instruction{ op: .port_io_collect, origin: reader.no_node }
449 Instruction{ op: .jump, a: 1, origin: reader.no_node }
450 // PortIoOperation 29
451 Instruction{ op: .port_io_enter, a: 29, origin: reader.no_node }
452 Instruction{ op: .port_io_step, origin: reader.no_node }
453 Instruction{ op: .port_io_collect, origin: reader.no_node }
454 Instruction{ op: .jump, a: 1, origin: reader.no_node }
455 // PortIoOperation 30
456 Instruction{ op: .port_io_enter, a: 30, origin: reader.no_node }
457 Instruction{ op: .port_io_step, origin: reader.no_node }
458 Instruction{ op: .port_io_collect, origin: reader.no_node }
459 Instruction{ op: .jump, a: 1, origin: reader.no_node }
460 // PortIoOperation 31
461 Instruction{ op: .port_io_enter, a: 31, origin: reader.no_node }
462 Instruction{ op: .port_io_step, origin: reader.no_node }
463 Instruction{ op: .port_io_collect, origin: reader.no_node }
464 Instruction{ op: .jump, a: 1, origin: reader.no_node }
465 // PortIoOperation 32
466 Instruction{ op: .port_io_enter, a: 32, origin: reader.no_node }
467 Instruction{ op: .port_io_step, origin: reader.no_node }
468 Instruction{ op: .port_io_collect, origin: reader.no_node }
469 Instruction{ op: .jump, a: 1, origin: reader.no_node }
470 // PortIoOperation 33
471 Instruction{ op: .port_io_enter, a: 33, origin: reader.no_node }
472 Instruction{ op: .port_io_step, origin: reader.no_node }
473 Instruction{ op: .port_io_collect, origin: reader.no_node }
474 Instruction{ op: .jump, a: 1, origin: reader.no_node }
475 // PortIoOperation 34
476 Instruction{ op: .port_io_enter, a: 34, origin: reader.no_node }
477 Instruction{ op: .port_io_step, origin: reader.no_node }
478 Instruction{ op: .port_io_collect, origin: reader.no_node }
479 Instruction{ op: .jump, a: 1, origin: reader.no_node }
480 // PortIoOperation 35
481 Instruction{ op: .port_io_enter, a: 35, origin: reader.no_node }
482 Instruction{ op: .port_io_step, origin: reader.no_node }
483 Instruction{ op: .port_io_collect, origin: reader.no_node }
484 Instruction{ op: .jump, a: 1, origin: reader.no_node }
485 // PortIoOperation 36
486 Instruction{ op: .port_io_enter, a: 36, origin: reader.no_node }
487 Instruction{ op: .port_io_step, origin: reader.no_node }
488 Instruction{ op: .port_io_collect, origin: reader.no_node }
489 Instruction{ op: .jump, a: 1, origin: reader.no_node }
490 // PortIoOperation 37
491 Instruction{ op: .port_io_enter, a: 37, origin: reader.no_node }
492 Instruction{ op: .port_io_step, origin: reader.no_node }
493 Instruction{ op: .port_io_collect, origin: reader.no_node }
494 Instruction{ op: .jump, a: 1, origin: reader.no_node }
495 // PortIoOperation 38
496 Instruction{ op: .port_io_enter, a: 38, origin: reader.no_node }
497 Instruction{ op: .port_io_step, origin: reader.no_node }
498 Instruction{ op: .port_io_collect, origin: reader.no_node }
499 Instruction{ op: .jump, a: 1, origin: reader.no_node }
500 // PortIoOperation 39
501 Instruction{ op: .port_io_enter, a: 39, origin: reader.no_node }
502 Instruction{ op: .port_io_step, origin: reader.no_node }
503 Instruction{ op: .port_io_collect, origin: reader.no_node }
504 Instruction{ op: .jump, a: 1, origin: reader.no_node }
505 // PortIoOperation 40
506 Instruction{ op: .port_io_enter, a: 40, origin: reader.no_node }
507 Instruction{ op: .port_io_step, origin: reader.no_node }
508 Instruction{ op: .port_io_collect, origin: reader.no_node }
509 Instruction{ op: .jump, a: 1, origin: reader.no_node }
510 // PortIoOperation 41
511 Instruction{ op: .port_io_enter, a: 41, origin: reader.no_node }
512 Instruction{ op: .port_io_step, origin: reader.no_node }
513 Instruction{ op: .port_io_collect, origin: reader.no_node }
514 Instruction{ op: .jump, a: 1, origin: reader.no_node }
515 // PortIoOperation 42
516 Instruction{ op: .port_io_enter, a: 42, origin: reader.no_node }
517 Instruction{ op: .port_io_step, origin: reader.no_node }
518 Instruction{ op: .port_io_collect, origin: reader.no_node }
519 Instruction{ op: .jump, a: 1, origin: reader.no_node }
520]!
521
522const custom_port_io_bridges = [
523 Function{
524 code: unsafe { nil }
525 params: 1
526 slot_count: 1
527 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[0], 4) }
528 }
529 Function{
530 code: unsafe { nil }
531 params: 1
532 slot_count: 1
533 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[4], 4) }
534 }
535 Function{
536 code: unsafe { nil }
537 params: 1
538 slot_count: 1
539 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[8], 4) }
540 }
541 Function{
542 code: unsafe { nil }
543 params: 1
544 slot_count: 1
545 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[12], 4) }
546 }
547 Function{
548 code: unsafe { nil }
549 params: 1
550 slot_count: 1
551 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[16], 4) }
552 }
553 Function{
554 code: unsafe { nil }
555 params: 1
556 slot_count: 1
557 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[20], 4) }
558 }
559 Function{
560 code: unsafe { nil }
561 params: 1
562 slot_count: 1
563 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[24], 4) }
564 }
565 Function{
566 code: unsafe { nil }
567 params: 1
568 slot_count: 1
569 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[28], 4) }
570 },
571 Function{
572 code: unsafe { nil }
573 params: 1
574 slot_count: 1
575 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[32], 4) }
576 }
577 Function{
578 code: unsafe { nil }
579 params: 1
580 slot_count: 1
581 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[36], 4) }
582 }
583 Function{
584 code: unsafe { nil }
585 params: 1
586 slot_count: 1
587 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[40], 4) }
588 }
589 Function{
590 code: unsafe { nil }
591 params: 1
592 slot_count: 1
593 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[44], 4) }
594 }
595 Function{
596 code: unsafe { nil }
597 params: 1
598 slot_count: 1
599 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[48], 4) }
600 }
601 Function{
602 code: unsafe { nil }
603 params: 1
604 slot_count: 1
605 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[52], 4) }
606 }
607 Function{
608 code: unsafe { nil }
609 params: 1
610 slot_count: 1
611 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[56], 4) }
612 }
613 Function{
614 code: unsafe { nil }
615 params: 1
616 slot_count: 1
617 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[60], 4) }
618 }
619 Function{
620 code: unsafe { nil }
621 params: 1
622 slot_count: 1
623 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[64], 4) }
624 }
625 Function{
626 code: unsafe { nil }
627 params: 1
628 slot_count: 1
629 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[68], 4) }
630 }
631 Function{
632 code: unsafe { nil }
633 params: 1
634 slot_count: 1
635 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[72], 4) }
636 }
637 Function{
638 code: unsafe { nil }
639 params: 1
640 slot_count: 1
641 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[76], 4) }
642 },
643 Function{
644 code: unsafe { nil }
645 params: 1
646 slot_count: 1
647 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[80], 4) }
648 }
649 Function{
650 code: unsafe { nil }
651 params: 1
652 slot_count: 1
653 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[84], 4) }
654 }
655 Function{
656 code: unsafe { nil }
657 params: 1
658 slot_count: 1
659 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[88], 4) }
660 }
661 Function{
662 code: unsafe { nil }
663 params: 1
664 slot_count: 1
665 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[92], 4) }
666 }
667 Function{
668 code: unsafe { nil }
669 params: 1
670 slot_count: 1
671 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[96], 4) }
672 }
673 Function{
674 code: unsafe { nil }
675 params: 1
676 slot_count: 1
677 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[100], 4) }
678 }
679 Function{
680 code: unsafe { nil }
681 params: 1
682 slot_count: 1
683 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[104], 4) }
684 }
685 Function{
686 code: unsafe { nil }
687 params: 1
688 slot_count: 1
689 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[108], 4) }
690 }
691 Function{
692 code: unsafe { nil }
693 params: 1
694 slot_count: 1
695 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[112], 4) }
696 }
697 Function{
698 code: unsafe { nil }
699 params: 1
700 slot_count: 1
701 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[116], 4) }
702 }
703 Function{
704 code: unsafe { nil }
705 params: 1
706 slot_count: 1
707 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[120], 4) }
708 }
709 Function{
710 code: unsafe { nil }
711 params: 1
712 slot_count: 1
713 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[124], 4) }
714 }
715 Function{
716 code: unsafe { nil }
717 params: 1
718 slot_count: 1
719 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[128], 4) }
720 }
721 Function{
722 code: unsafe { nil }
723 params: 1
724 slot_count: 1
725 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[132], 4) }
726 }
727 Function{
728 code: unsafe { nil }
729 params: 1
730 slot_count: 1
731 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[136], 4) }
732 }
733 Function{
734 code: unsafe { nil }
735 params: 1
736 slot_count: 1
737 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[140], 4) }
738 }
739 Function{
740 code: unsafe { nil }
741 params: 1
742 slot_count: 1
743 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[144], 4) }
744 }
745 Function{
746 code: unsafe { nil }
747 params: 1
748 slot_count: 1
749 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[148], 4) }
750 }
751 Function{
752 code: unsafe { nil }
753 params: 1
754 slot_count: 1
755 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[152], 4) }
756 }
757 Function{
758 code: unsafe { nil }
759 params: 1
760 slot_count: 1
761 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[156], 4) }
762 },
763 Function{
764 code: unsafe { nil }
765 params: 1
766 slot_count: 1
767 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[160], 4) }
768 },
769 Function{
770 code: unsafe { nil }
771 params: 1
772 slot_count: 1
773 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[164], 4) }
774 },
775 Function{
776 code: unsafe { nil }
777 params: 1
778 slot_count: 1
779 instructions: unsafe { static_bridge_instructions(&custom_port_io_code[168], 4) }
780 },
781]!
782
783fn check_custom_port_arity(operation PortIoOperation, count int) ! {
784 minimum, maximum := match operation {
785 .write_u8, .write_char, .display_text, .write_datum, .write_shared, .write_simple,
786 .read_text {
787 1, 2
788 }
789 .write_bytes, .read_into, .write_text { 1, 4 }
790 .put_u8, .get_bytes, .set_position, .get_text, .put_char, .put_datum { 2, 2 }
791 .put_bytes, .put_text { 2, 4 }
792 .read_u8, .peek_u8, .read_char, .peek_char, .read_line, .newline, .read_datum,
793 .flush_output {
794 0, 1
795 }
796 .read_bytes { 1, 2 }
797 .get_into, .text_into { 4, 4 }
798 else { 1, 1 }
799 }
800 check_arity(port_io_names[int(operation)], count, minimum, maximum)!
801}
802