konilo

Check-in [0d2723f940]
Login

Check-in [0d2723f940]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:merge ilo-wasm.wat, a webassembly version of ilo for wasmtime
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0d2723f9408770fdf7f7c893e4dd31228511c8f231f5f6d89c856b458b2e4b46
User & Date: crc 2026-06-26 12:51:04.135
Context
2026-06-26
12:57
merge vhdl implementation into the tree; simulation with ghdl works; not tested on actual hardware yet Leaf check-in: 95e971fd44 user: crc tags: trunk
12:51
merge ilo-wasm.wat, a webassembly version of ilo for wasmtime check-in: 0d2723f940 user: crc tags: trunk
2026-06-18
19:29
add a fortran 77 version of ilo check-in: 3b3b908db7 user: crc tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to RELEASE.txt.
45
46
47
48
49
50
51

52
53
54
55
56
57
58
  - Python
  - RetroForth
  - Rust
  - Scheme
  - Smalltalk
  - Swift
  - TypeScript

  - Zig

  - x86-native (C & assembly)
  - x86 assembly (DOS)

- Precompiled binaries for ilo for a variety of hosts:








>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  - Python
  - RetroForth
  - Rust
  - Scheme
  - Smalltalk
  - Swift
  - TypeScript
  - WebAssembly
  - Zig

  - x86-native (C & assembly)
  - x86 assembly (DOS)

- Precompiled binaries for ilo for a variety of hosts:

185
186
187
188
189
190
191




192
193
194
195
196
197
198

  - 8088/8086 implementation in NASM, for MS-DOS

- ilo.f

  - new implementation: gfortran, Fortran 77





IMAGE

- dictionary data now in konilo.pali instead of being built by
  separate preprocessor
- inlined use of `tuck` (as `i dupuswpo`) at various points
- `n:to-s` save a call/return per character
- `n:limit` now falls through into `n:max`, saving a jump







>
>
>
>







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

  - 8088/8086 implementation in NASM, for MS-DOS

- ilo.f

  - new implementation: gfortran, Fortran 77

- ilo-wasm.wat

  - new implementation (webassembly; for wasmtime)

IMAGE

- dictionary data now in konilo.pali instead of being built by
  separate preprocessor
- inlined use of `tuck` (as `i dupuswpo`) at various points
- `n:to-s` save a call/return per character
- `n:limit` now falls through into `n:max`, saving a jump
Added ilo-vm/ilo-wasm.wat.




























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
(module
  ;; Run with Wasmtime from a directory containing ilo.rom and ilo.blocks:
  ;;   XDG_CACHE_HOME=/tmp /home/crc/.wasmtime/bin/wasmtime --dir=$PWD::. extras/ilo-wasm.wat
  ;;
  ;; The module uses WASI preview1 and fixed guest paths "ilo.rom" and
  ;; "ilo.blocks", matching the default names used by ilo.c.
  (import "wasi_snapshot_preview1" "fd_read"
    (func $fd_read (param i32 i32 i32 i32) (result i32)))
  (import "wasi_snapshot_preview1" "fd_write"
    (func $fd_write (param i32 i32 i32 i32) (result i32)))
  (import "wasi_snapshot_preview1" "fd_close"
    (func $fd_close (param i32) (result i32)))
  (import "wasi_snapshot_preview1" "fd_seek"
    (func $fd_seek (param i32 i64 i32 i32) (result i32)))
  (import "wasi_snapshot_preview1" "path_open"
    (func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))

  (memory 5)
  (export "memory" (memory 0))
  (export "_start" (func $_start))

  ;; Memory map:
  ;;   0..262143      ilo memory: 65,536 signed 32-bit cells
  ;;   262144..262275 data stack: 33 signed 32-bit cells
  ;;   262276..263303 address stack: 257 signed 32-bit cells
  (global $DS i32 (i32.const 262144))
  (global $AS i32 (i32.const 262276))
  (global $OPENED i32 (i32.const 263304))
  (global $NIO i32 (i32.const 263308))
  (global $IOV i32 (i32.const 263312))
  (global $SEEKED i32 (i32.const 263320))
  (global $BYTE i32 (i32.const 263328))
  (global $NUMBUF i32 (i32.const 263336))
  (global $ROM i32 (i32.const 263384))
  (global $BLOCKS i32 (i32.const 263392))

  (data (i32.const 263384) "ilo.rom\00")
  (data (i32.const 263392) "ilo.blocks\00")

  (global $ip (mut i32) (i32.const 0))
  (global $sp (mut i32) (i32.const 0))
  (global $rp (mut i32) (i32.const 0))

  (func $cell (param $a i32) (result i32)
    local.get $a
    i32.const 2
    i32.shl)

  (func $ds_addr (param $a i32) (result i32)
    global.get $DS
    local.get $a
    i32.const 2
    i32.shl
    i32.add)

  (func $as_addr (param $a i32) (result i32)
    global.get $AS
    local.get $a
    i32.const 2
    i32.shl
    i32.add)

  (func $t_addr (result i32)
    global.get $sp
    call $ds_addr)

  (func $n_addr (result i32)
    global.get $sp
    i32.const 1
    i32.sub
    call $ds_addr)

  (func $r_addr (result i32)
    global.get $rp
    call $as_addr)

  (func $t (result i32)
    call $t_addr
    i32.load)

  (func $n (result i32)
    call $n_addr
    i32.load)

  (func $set_t (param $v i32)
    call $t_addr
    local.get $v
    i32.store)

  (func $set_n (param $v i32)
    call $n_addr
    local.get $v
    i32.store)

  (func $push (param $v i32)
    global.get $sp
    i32.const 1
    i32.add
    call $ds_addr
    local.get $v
    i32.store
    global.get $sp
    i32.const 1
    i32.add
    global.set $sp)

  (func $pop (result i32)
    (local $a i32)
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp
    global.get $sp
    i32.const 1
    i32.add
    local.tee $a
    call $ds_addr
    i32.load)

  (func $open_ro (param $path i32) (param $len i32) (result i32)
    (local $err i32)
    i32.const 3            ;; cwd preopen
    i32.const 0
    local.get $path
    local.get $len
    i32.const 0
    i64.const 6            ;; FD_READ | FD_SEEK
    i64.const 0
    i32.const 0
    global.get $OPENED
    call $path_open
    local.tee $err
    if
      i32.const -1
      return
    end
    global.get $OPENED
    i32.load)

  (func $open_rw (param $path i32) (param $len i32) (result i32)
    (local $err i32)
    i32.const 3
    i32.const 0
    local.get $path
    local.get $len
    i32.const 0
    i64.const 70           ;; FD_READ | FD_SEEK | FD_WRITE
    i64.const 0
    i32.const 0
    global.get $OPENED
    call $path_open
    local.tee $err
    if
      i32.const -1
      return
    end
    global.get $OPENED
    i32.load)

  (func $load_image
    (local $fd i32)
    global.get $ROM
    i32.const 7
    call $open_ro
    local.set $fd
    local.get $fd
    i32.const 0
    i32.lt_s
    if
      i32.const 0
      global.set $ip
      i32.const 0
      global.set $sp
      i32.const 0
      global.set $rp
      return
    end
    global.get $IOV
    i32.const 0
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    i32.const 262144
    i32.store
    local.get $fd
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_read
    drop
    local.get $fd
    call $fd_close
    drop
    i32.const 0
    global.set $ip
    i32.const 0
    global.set $sp
    i32.const 0
    global.set $rp)

  (func $save_image
    (local $fd i32)
    global.get $ROM
    i32.const 7
    call $open_rw
    local.set $fd
    local.get $fd
    i32.const 0
    i32.lt_s
    if
      return
    end
    global.get $IOV
    i32.const 0
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    i32.const 262144
    i32.store
    local.get $fd
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_write
    drop
    local.get $fd
    call $fd_close
    drop)

  (func $read_block
    (local $fd i32) (local $block i32) (local $buffer i32)
    call $pop
    local.set $buffer
    call $pop
    local.set $block
    global.get $BLOCKS
    i32.const 10
    call $open_ro
    local.set $fd
    local.get $fd
    i32.const 0
    i32.lt_s
    if
      return
    end
    local.get $fd
    local.get $block
    i64.extend_i32_s
    i64.const 4096
    i64.mul
    i32.const 0
    global.get $SEEKED
    call $fd_seek
    drop
    global.get $IOV
    local.get $buffer
    call $cell
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    i32.const 4096
    i32.store
    local.get $fd
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_read
    drop
    local.get $fd
    call $fd_close
    drop)

  (func $write_block
    (local $fd i32) (local $block i32) (local $buffer i32)
    call $pop
    local.set $buffer
    call $pop
    local.set $block
    global.get $BLOCKS
    i32.const 10
    call $open_rw
    local.set $fd
    local.get $fd
    i32.const 0
    i32.lt_s
    if
      return
    end
    local.get $fd
    local.get $block
    i64.extend_i32_s
    i64.const 4096
    i64.mul
    i32.const 0
    global.get $SEEKED
    call $fd_seek
    drop
    global.get $IOV
    local.get $buffer
    call $cell
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    i32.const 4096
    i32.store
    local.get $fd
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_write
    drop
    local.get $fd
    call $fd_close
    drop)

  (func $save_ip
    global.get $rp
    i32.const 1
    i32.add
    global.set $rp
    call $r_addr
    global.get $ip
    i32.store)

  (func $li
    global.get $ip
    i32.const 1
    i32.add
    global.set $ip
    global.get $ip
    call $cell
    i32.load
    call $push)

  (func $du
    call $t
    call $push)

  (func $dr
    call $t_addr
    i32.const 0
    i32.store
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $sw
    (local $a i32)
    call $t
    local.set $a
    call $n
    call $set_t
    local.get $a
    call $set_n)

  (func $pu
    global.get $rp
    i32.const 1
    i32.add
    global.set $rp
    call $r_addr
    call $pop
    i32.store)

  (func $po
    call $r_addr
    i32.load
    call $push
    global.get $rp
    i32.const 1
    i32.sub
    global.set $rp)

  (func $ju
    call $pop
    i32.const 1
    i32.sub
    global.set $ip)

  (func $ca
    call $save_ip
    call $pop
    i32.const 1
    i32.sub
    global.set $ip)

  (func $cc
    (local $a i32)
    call $pop
    local.set $a
    call $pop
    if
      call $save_ip
      local.get $a
      i32.const 1
      i32.sub
      global.set $ip
    end)

  (func $cj
    (local $a i32)
    call $pop
    local.set $a
    call $pop
    if
      local.get $a
      i32.const 1
      i32.sub
      global.set $ip
    end)

  (func $re
    call $r_addr
    i32.load
    global.set $ip
    global.get $rp
    i32.const 1
    i32.sub
    global.set $rp)

  (func $eq
    call $n
    call $t
    i32.eq
    i32.const -1
    i32.mul
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $ne
    call $n
    call $t
    i32.ne
    i32.const -1
    i32.mul
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $lt
    call $n
    call $t
    i32.lt_s
    i32.const -1
    i32.mul
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $gt
    call $n
    call $t
    i32.gt_s
    i32.const -1
    i32.mul
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $fe
    call $t
    call $cell
    i32.load
    call $set_t)

  (func $st
    call $t
    call $cell
    call $n
    i32.store
    global.get $sp
    i32.const 2
    i32.sub
    global.set $sp)

  (func $ad
    call $n
    call $t
    i32.add
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $su
    call $n
    call $t
    i32.sub
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $mu
    call $n
    call $t
    i32.mul
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $di
    (local $a i32) (local $b i32)
    call $t
    local.set $a
    call $n
    local.set $b
    local.get $b
    local.get $a
    i32.div_s
    call $set_t
    local.get $b
    local.get $a
    i32.rem_s
    call $set_n
    local.get $b
    i32.const 0
    i32.ge_s
    call $n
    i32.const 0
    i32.lt_s
    i32.and
    if
      call $t
      i32.const 1
      i32.add
      call $set_t
      call $n
      local.get $a
      i32.sub
      call $set_n
    end)

  (func $an
    call $t
    call $n
    i32.and
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $or
    call $t
    call $n
    i32.or
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $xo
    call $t
    call $n
    i32.xor
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $sl
    call $n
    call $t
    i32.shl
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $sr
    call $n
    call $t
    i32.shr_s
    call $set_n
    global.get $sp
    i32.const 1
    i32.sub
    global.set $sp)

  (func $cp
    (local $l i32) (local $d i32) (local $s i32)
    call $pop
    local.set $l
    call $pop
    local.set $d
    call $t
    local.set $s
    i32.const -1
    call $set_t
    block $done
      loop $again
        local.get $l
        i32.eqz
        br_if $done
        local.get $d
        call $cell
        i32.load
        local.get $s
        call $cell
        i32.load
        i32.ne
        if
          i32.const 0
          call $set_t
        end
        local.get $l
        i32.const 1
        i32.sub
        local.set $l
        local.get $s
        i32.const 1
        i32.add
        local.set $s
        local.get $d
        i32.const 1
        i32.add
        local.set $d
        br $again
      end
    end)

  (func $cy
    (local $l i32) (local $d i32) (local $s i32)
    call $pop
    local.set $l
    call $pop
    local.set $d
    call $pop
    local.set $s
    block $done
      loop $again
        local.get $l
        i32.eqz
        br_if $done
        local.get $d
        call $cell
        local.get $s
        call $cell
        i32.load
        i32.store
        local.get $l
        i32.const 1
        i32.sub
        local.set $l
        local.get $s
        i32.const 1
        i32.add
        local.set $s
        local.get $d
        i32.const 1
        i32.add
        local.set $d
        br $again
      end
    end)

  (func $write_bytes (param $ptr i32) (param $len i32)
    global.get $IOV
    local.get $ptr
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    local.get $len
    i32.store
    i32.const 1
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_write
    drop)

  (func $write_byte (param $b i32)
    global.get $BYTE
    local.get $b
    i32.store8
    global.get $BYTE
    i32.const 1
    call $write_bytes)

  (func $read_byte (result i32)
    global.get $IOV
    global.get $BYTE
    i32.store
    global.get $IOV
    i32.const 4
    i32.add
    i32.const 1
    i32.store
    i32.const 0
    global.get $IOV
    i32.const 1
    global.get $NIO
    call $fd_read
    drop
    global.get $NIO
    i32.load
    i32.eqz
    if
      i32.const 32
      return
    end
    global.get $BYTE
    i32.load8_u)

  (func $io
    (local $dev i32)
    call $pop
    local.set $dev
    local.get $dev
    i32.eqz
    if
      call $pop
      call $write_byte
      return
    end
    local.get $dev
    i32.const 1
    i32.eq
    if
      call $read_byte
      call $push
      return
    end
    local.get $dev
    i32.const 2
    i32.eq
    if
      call $read_block
      return
    end
    local.get $dev
    i32.const 3
    i32.eq
    if
      call $write_block
      return
    end
    local.get $dev
    i32.const 4
    i32.eq
    if
      call $save_image
      return
    end
    local.get $dev
    i32.const 5
    i32.eq
    if
      call $load_image
      i32.const -1
      global.set $ip
      return
    end
    local.get $dev
    i32.const 6
    i32.eq
    if
      i32.const 65536
      global.set $ip
      return
    end
    local.get $dev
    i32.const 7
    i32.eq
    if
      global.get $sp
      call $push
      global.get $rp
      call $push
    end)

  (func $process (param $o i32)
    local.get $o
    i32.const 1
    i32.eq
    if call $li return end
    local.get $o
    i32.const 2
    i32.eq
    if call $du return end
    local.get $o
    i32.const 3
    i32.eq
    if call $dr return end
    local.get $o
    i32.const 4
    i32.eq
    if call $sw return end
    local.get $o
    i32.const 5
    i32.eq
    if call $pu return end
    local.get $o
    i32.const 6
    i32.eq
    if call $po return end
    local.get $o
    i32.const 7
    i32.eq
    if call $ju return end
    local.get $o
    i32.const 8
    i32.eq
    if call $ca return end
    local.get $o
    i32.const 9
    i32.eq
    if call $cc return end
    local.get $o
    i32.const 10
    i32.eq
    if call $cj return end
    local.get $o
    i32.const 11
    i32.eq
    if call $re return end
    local.get $o
    i32.const 12
    i32.eq
    if call $eq return end
    local.get $o
    i32.const 13
    i32.eq
    if call $ne return end
    local.get $o
    i32.const 14
    i32.eq
    if call $lt return end
    local.get $o
    i32.const 15
    i32.eq
    if call $gt return end
    local.get $o
    i32.const 16
    i32.eq
    if call $fe return end
    local.get $o
    i32.const 17
    i32.eq
    if call $st return end
    local.get $o
    i32.const 18
    i32.eq
    if call $ad return end
    local.get $o
    i32.const 19
    i32.eq
    if call $su return end
    local.get $o
    i32.const 20
    i32.eq
    if call $mu return end
    local.get $o
    i32.const 21
    i32.eq
    if call $di return end
    local.get $o
    i32.const 22
    i32.eq
    if call $an return end
    local.get $o
    i32.const 23
    i32.eq
    if call $or return end
    local.get $o
    i32.const 24
    i32.eq
    if call $xo return end
    local.get $o
    i32.const 25
    i32.eq
    if call $sl return end
    local.get $o
    i32.const 26
    i32.eq
    if call $sr return end
    local.get $o
    i32.const 27
    i32.eq
    if call $cp return end
    local.get $o
    i32.const 28
    i32.eq
    if call $cy return end
    local.get $o
    i32.const 29
    i32.eq
    if call $io return end)

  (func $process_bundle (param $opcode i32)
    local.get $opcode
    i32.const 255
    i32.and
    call $process
    local.get $opcode
    i32.const 8
    i32.shr_u
    i32.const 255
    i32.and
    call $process
    local.get $opcode
    i32.const 16
    i32.shr_u
    i32.const 255
    i32.and
    call $process
    local.get $opcode
    i32.const 24
    i32.shr_u
    i32.const 255
    i32.and
    call $process)

  (func $execute
    block $done
      loop $again
        global.get $ip
        i32.const 65536
        i32.ge_s
        br_if $done
        global.get $ip
        call $cell
        i32.load
        call $process_bundle
        global.get $ip
        i32.const 1
        i32.add
        global.set $ip
        br $again
      end
    end)

  (func $print_i32 (param $v i32)
    (local $u i32) (local $p i32) (local $end i32)
    local.get $v
    i32.eqz
    if
      i32.const 48
      call $write_byte
      return
    end
    local.get $v
    i32.const 0
    i32.lt_s
    if
      i32.const 45
      call $write_byte
      i32.const 0
      local.get $v
      i32.sub
      local.set $u
    else
      local.get $v
      local.set $u
    end
    global.get $NUMBUF
    i32.const 31
    i32.add
    local.tee $p
    local.set $end
    block $done
      loop $digits
        local.get $p
        local.get $u
        i32.const 10
        i32.rem_u
        i32.const 48
        i32.add
        i32.store8
        local.get $u
        i32.const 10
        i32.div_u
        local.set $u
        local.get $u
        i32.eqz
        br_if $done
        local.get $p
        i32.const 1
        i32.sub
        local.set $p
        br $digits
      end
    end
    local.get $p
    local.get $end
    local.get $p
    i32.sub
    i32.const 1
    i32.add
    call $write_bytes)

  (func $dump_stack
    block $done
      loop $again
        global.get $sp
        i32.eqz
        br_if $done
        i32.const 32
        call $write_byte
        call $t
        call $print_i32
        global.get $sp
        i32.const 1
        i32.sub
        global.set $sp
        br $again
      end
    end
    i32.const 10
    call $write_byte)

  (func $_start
    call $load_image
    call $execute
    call $dump_stack))