-
Notifications
You must be signed in to change notification settings - Fork 5
/
containers.wat
1822 lines (1531 loc) · 65.4 KB
/
containers.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
;;========================================================;;
;; CONTAINER TYPES WITH HANDWRITTEN WEBASSEMBLY ;;
;; `containers.wat` Lingdong Huang 2020 Public Domain ;;
;;========================================================;;
;; array<T> + (linked) list<T> + (hash) map<T,T> ;;
;;--------------------------------------------------------;;
(module
;; This file contains implementations for:
;;---------------------------------------------------------
;; - ARRAYS (prefix: arr_*)
;; Continous, resizable storage for a sequence of values,
;; similar to C++ vector<T>
;;
;; +------------------------------+
;; |data|length|elem_size|capacity|
;; +-|----------------------------+
;; | +---------------------------
;; `------> |elem 0|elem 1|elem 2|......
;; +---------------------------
;;---------------------------------------------------------
;; - LISTS (prefix: list_*)
;; Doubly linked list, similar to C++ list<T>
;;
;; +--------------------------+
;; |head|tail|length|elem_size|
;; +-|----|-------------------+
;; | `--------------------------------------------.
;; | .---------. .-------... |
;; | +------|-------+ | +------|-------+ `-->+--------------+
;; `-------->|prev|next|data| `->|prev|next|data| ...... |prev|next|data|
;; +--------------+ +-|------------+ +-|------------+
;; ^---------------------' ....-----'
;;---------------------------------------------------------
;; - MAPS (prefix: map_*)
;; Hash table (separate chaining with linked lists),
;; similar to C++ map<T,T>.
;;
;; Both size of key and size of value can be variable within
;; the same hash table. In otherwords, it maps from any
;; sequence of bytes to another arbitrary sequence of bytes.
;;
;; Functions involving keys have two versions, *_i and *_h.
;; _i takes an i32 as key directly (for simple small keys),
;; while _h versions read the key from the heap given a
;; pointer and a byte count (for larger keys)
;;
;; +-----------+
;; |num_buckets| ,-------------------------------.
;; |-----------| +-|----------------------------+ | +------------------------------+
;; | bucket 0 |------->|next|key_size|key|val_size|val| `->|next|key_size|key|val_size|val|
;; |-----------| +------------------------------+ +------------------------------+
;; | bucket 1 |
;; |-----------| +------------------------------+
;; | bucket 2 |------->|next|key_size|key|val_size|val|
;; |-----------| +------------------------------+
;; | ......... |
;;---------------------------------------------------------
;; The container implementations are polymorphic, meaning that
;; the elements can be any type, (bytes, ints, floats, structs...)
;; However, since WebAssembly does not have a syntax for user
;; types/structs, the functions cannot take them as parameters
;; and write them into the container for you -- Therefore,
;; functions that're otherwise supposed to write values to
;; containers will return a pointer to the appropriate memory
;; location instead, and user need to supply custom code to do
;; the former. E.g.
;;
;; ;; new array with element-size = 4 bytes (e.g. i32)
;; (local.set $a (call $arr_new (i32.const 4)))
;;
;; ;; 'push' adds an element at the end (does not write the
;; ;; actual value, but returns a pointer to the element)
;; (local.set $ptr (call $arr_push (local.get $a)))
;;
;; ;; user writes the value (e.g. 42) given the pointer
;; (i32.store (local.get $ptr) (i32.const 42))
;; Each container type is documented in more detail near
;; respective implementations
(global $DEFAULT_CAPACITY (mut i32) (i32.const 8))
;;========================================================;;
;; BASELINE MALLOC WITH HANDWRITTEN WEBASSEMBLY ;;
;;========================================================;;
;; 32-bit implicit-free-list first-fit baseline malloc ;;
;;--------------------------------------------------------;;
;; IMPLICIT FREE LIST:
;; Worse utilization and throughput than explicit/segregated, but easier
;; to implement :P
;;
;; HEAP LO HEAP HI
;; +---------------------+---------------------+...+---------------------+
;; | HDR | PAYLOAD | FTR | HDR | PAYLOAD | FTR |...+ HDR | PAYLOAD | FTR |
;; +----------^----------+---------------------+...+---------------------+
;; |_ i.e. user data
;;
;; LAYOUT OF A BLOCK:
;; Since memory is aligned to multiple of 4 bytes, the last two bits of
;; payload_size is redundant. Therefore the last bit of header is used to
;; store the is_free flag.
;;
;; |---- HEADER (4b)----
;; | ,--payload size (x4)--. ,-is free?
;; | 0b . . . . . . . . . . . . 0 0
;; |------ PAYLOAD -----
;; |
;; | user data (N x 4b)
;; |
;; |---- FOOTER (4b)---- (duplicate of header)
;; | ,--payload size (x4)--. ,-is free?
;; | 0b . . . . . . . . . . . . 0 0
;; |--------------------
;;
;; FORMULAS:
;; (these formulas are used throughout the code, so they're listed here
;; instead of explained each time encountered)
;;
;; payload_size = block_size - (header_size + footer_size) = block_size - 8
;;
;; payload_pointer = header_pointer + header_size = header_pointer + 4
;;
;; footer_pointer = header_pointer + header_size + payload_size
;; = (header_pointer + payload_size) + 4
;;
;; next_header_pointer = footer_pointer + footer_size = footer_pointer + 4
;;
;; prev_footer_pointer = header_pointer - footer_size = header_pointer - 4
(memory $mem 1) ;; start with 1 page (64K)
(global $max_addr (mut i32) (i32.const 65536)) ;; initial heap size (64K)
(global $did_init (mut i32) (i32.const 0)) ;; init() called?
;; helpers to pack/unpack payload_size/is_free from header/footer
;; by masking out bits
;; read payload_size from header/footer given pointer to header/footer
(func $hdr_get_size (param $ptr i32) (result i32)
(i32.and (i32.load (local.get $ptr)) (i32.const 0xFFFFFFFC))
)
;; read is_free from header/footer
(func $hdr_get_free (param $ptr i32) (result i32)
(i32.and (i32.load (local.get $ptr)) (i32.const 0x00000001))
)
;; write payload_size to header/footer
(func $hdr_set_size (param $ptr i32) (param $n i32)
(i32.store (local.get $ptr) (i32.or
(i32.and (i32.load (local.get $ptr)) (i32.const 0x00000003))
(local.get $n)
))
)
;; write is_free to header/footer
(func $hdr_set_free (param $ptr i32) (param $n i32)
(i32.store (local.get $ptr) (i32.or
(i32.and (i32.load (local.get $ptr)) (i32.const 0xFFFFFFFE))
(local.get $n)
))
)
;; align memory by 4 bytes
(func $align4 (param $x i32) (result i32)
(i32.and
(i32.add (local.get $x) (i32.const 3))
(i32.const -4)
)
)
;; initialize heap
;; make the whole heap a big free block
;; - automatically invoked by first malloc() call
;; - can be manually called to nuke the whole heap
(func $init
;; write payload_size to header and footer
(call $hdr_set_size (i32.const 0) (i32.sub (global.get $max_addr) (i32.const 8)))
(call $hdr_set_size (i32.sub (global.get $max_addr) (i32.const 4))
(i32.sub (global.get $max_addr) (i32.const 8))
)
;; write is_free to header and footer
(call $hdr_set_free (i32.const 0) (i32.const 1))
(call $hdr_set_free (i32.sub (global.get $max_addr) (i32.const 4)) (i32.const 1))
;; set flag to tell malloc() that we've already called init()
(global.set $did_init (i32.const 1))
)
;; extend (grow) the heap (to accomodate more blocks)
;; parameter: number of pages (64K) to grow
;; - automatically invoked by malloc() when current heap has insufficient free space
;; - can be manually called to get more space in advance
(func $extend (param $n_pages i32)
(local $n_bytes i32)
(local $ftr i32)
(local $prev_ftr i32)
(local $prev_hdr i32)
(local $prev_size i32)
(local.set $prev_ftr (i32.sub (global.get $max_addr) (i32.const 4)) )
;; compute number of bytes from page count (1page = 64K = 65536bytes)
(local.set $n_bytes (i32.mul (local.get $n_pages) (i32.const 65536)))
;; system call to grow memory (`drop` discards the (useless) return value of memory.grow)
(drop (memory.grow (local.get $n_pages) ))
;; make the newly acquired memory a big free block
(call $hdr_set_size (global.get $max_addr) (i32.sub (local.get $n_bytes) (i32.const 8)))
(call $hdr_set_free (global.get $max_addr) (i32.const 1))
(global.set $max_addr (i32.add (global.get $max_addr) (local.get $n_bytes) ))
(local.set $ftr (i32.sub (global.get $max_addr) (i32.const 4)))
(call $hdr_set_size (local.get $ftr)
(i32.sub (local.get $n_bytes) (i32.const 8))
)
(call $hdr_set_free (local.get $ftr) (i32.const 1))
;; see if we can join the new block with the last block of the old heap
(if (i32.eqz (call $hdr_get_free (local.get $prev_ftr)))(then)(else
;; the last block is free, join it.
(local.set $prev_size (call $hdr_get_size (local.get $prev_ftr)))
(local.set $prev_hdr
(i32.sub (i32.sub (local.get $prev_ftr) (local.get $prev_size)) (i32.const 4))
)
(call $hdr_set_size (local.get $prev_hdr)
(i32.add (local.get $prev_size) (local.get $n_bytes) )
)
(call $hdr_set_size (local.get $ftr)
(i32.add (local.get $prev_size) (local.get $n_bytes) )
)
))
)
;; find a free block that fit the request number of bytes
;; modifies the heap once a candidate is found
;; first-fit: not the best policy, but the simplest
(func $find (param $n_bytes i32) (result i32)
(local $ptr i32)
(local $size i32)
(local $is_free i32)
(local $pay_ptr i32)
(local $rest i32)
;; loop through all blocks
(local.set $ptr (i32.const 0))
loop $search
;; we reached the end of heap and haven't found anything, return NULL
(if (i32.lt_u (local.get $ptr) (global.get $max_addr))(then)(else
(i32.const 0)
return
))
;; read info about current block
(local.set $size (call $hdr_get_size (local.get $ptr)))
(local.set $is_free (call $hdr_get_free (local.get $ptr)))
(local.set $pay_ptr (i32.add (local.get $ptr) (i32.const 4) ))
;; check if the current block is free
(if (i32.eq (local.get $is_free) (i32.const 1))(then
;; it's free, but too small, move on
(if (i32.gt_u (local.get $n_bytes) (local.get $size))(then
(local.set $ptr (i32.add (local.get $ptr) (i32.add (local.get $size) (i32.const 8))))
(br $search)
;; it's free, and large enough to be split into two blocks
)(else(if (i32.lt_u (local.get $n_bytes) (i32.sub (local.get $size) (i32.const 8)))(then
;; OLD HEAP
;; ...+-------------------------------------------+...
;; ...| HDR | FREE | FTR |...
;; ...+-------------------------------------------+...
;; NEW HEAP
;; ...+---------------------+---------------------+...
;; ...| HDR | ALLOC | FTR | HDR | FREE | FTR |...
;; ...+---------------------+---------------------+...
;; size of the remaining half
(local.set $rest (i32.sub (i32.sub (local.get $size) (local.get $n_bytes) ) (i32.const 8)))
;; update headers and footers to reflect the change (see FORMULAS)
(call $hdr_set_size (local.get $ptr) (local.get $n_bytes))
(call $hdr_set_free (local.get $ptr) (i32.const 0))
(call $hdr_set_size (i32.add (i32.add (local.get $ptr) (local.get $n_bytes)) (i32.const 4))
(local.get $n_bytes)
)
(call $hdr_set_free (i32.add (i32.add (local.get $ptr) (local.get $n_bytes)) (i32.const 4))
(i32.const 0)
)
(call $hdr_set_size (i32.add (i32.add (local.get $ptr) (local.get $n_bytes)) (i32.const 8))
(local.get $rest)
)
(call $hdr_set_free (i32.add (i32.add (local.get $ptr) (local.get $n_bytes)) (i32.const 8))
(i32.const 1)
)
(call $hdr_set_size (i32.add (i32.add (local.get $ptr) (local.get $size)) (i32.const 4))
(local.get $rest)
)
(local.get $pay_ptr)
return
)(else
;; the block is free, but not large enough to be split into two blocks
;; we return the whole block as one
(call $hdr_set_free (local.get $ptr) (i32.const 0))
(call $hdr_set_free (i32.add (i32.add (local.get $ptr) (local.get $size)) (i32.const 4))
(i32.const 0)
)
(local.get $pay_ptr)
return
))))
)(else
;; the block is not free, we move on to the next block
(local.set $ptr (i32.add (local.get $ptr) (i32.add (local.get $size) (i32.const 8))))
(br $search)
))
end
;; theoratically we will not reach here
;; return NULL
(i32.const 0)
)
;; malloc - allocate the requested number of bytes on the heap
;; returns a pointer to the block of memory allocated
;; returns NULL (0) when OOM
;; if heap is not large enough, grows it via extend()
(func $malloc (param $n_bytes i32) (result i32)
(local $ptr i32)
(local $n_pages i32)
;; call init() if we haven't done so yet
(if (i32.eqz (global.get $did_init)) (then
(call $init)
))
;; payload size is aligned to multiple of 4
(local.set $n_bytes (call $align4 (local.get $n_bytes)))
;; attempt allocation
(local.set $ptr (call $find (local.get $n_bytes)) )
;; NULL -> OOM -> extend heap
(if (i32.eqz (local.get $ptr))(then
;; compute # of pages from # of bytes, rounding up
(local.set $n_pages
(i32.div_u
(i32.add (local.get $n_bytes) (i32.const 65527) )
(i32.const 65528)
)
)
(call $extend (local.get $n_pages))
;; try again
(local.set $ptr (call $find (local.get $n_bytes)) )
))
(local.get $ptr)
)
;; free - free an allocated block given a pointer to it
(func $free (param $ptr i32)
(local $hdr i32)
(local $ftr i32)
(local $size i32)
(local $prev_hdr i32)
(local $prev_ftr i32)
(local $prev_size i32)
(local $prev_free i32)
(local $next_hdr i32)
(local $next_ftr i32)
(local $next_size i32)
(local $next_free i32)
;; step I: mark the block as free
(local.set $hdr (i32.sub (local.get $ptr) (i32.const 4)))
(local.set $size (call $hdr_get_size (local.get $hdr)))
(local.set $ftr (i32.add (i32.add (local.get $hdr) (local.get $size)) (i32.const 4)))
(call $hdr_set_free (local.get $hdr) (i32.const 1))
(call $hdr_set_free (local.get $ftr) (i32.const 1))
;; step II: try coalasce
;; coalasce with previous block
;; check that we're not already the first block
(if (i32.eqz (local.get $hdr)) (then)(else
;; read info about previous block
(local.set $prev_ftr (i32.sub (local.get $hdr) (i32.const 4)))
(local.set $prev_size (call $hdr_get_size (local.get $prev_ftr)))
(local.set $prev_hdr
(i32.sub (i32.sub (local.get $prev_ftr) (local.get $prev_size)) (i32.const 4))
)
;; check if previous block is free -> merge them
(if (i32.eqz (call $hdr_get_free (local.get $prev_ftr))) (then) (else
(local.set $size (i32.add (i32.add (local.get $size) (local.get $prev_size)) (i32.const 8)))
(call $hdr_set_size (local.get $prev_hdr) (local.get $size))
(call $hdr_set_size (local.get $ftr) (local.get $size))
;; set current header pointer to previous header
(local.set $hdr (local.get $prev_hdr))
))
))
;; coalasce with next block
(local.set $next_hdr (i32.add (local.get $ftr) (i32.const 4)))
;; check that we're not already the last block
(if (i32.eq (local.get $next_hdr) (global.get $max_addr)) (then)(else
;; read info about next block
(local.set $next_size (call $hdr_get_size (local.get $next_hdr)))
(local.set $next_ftr
(i32.add (i32.add (local.get $next_hdr) (local.get $next_size)) (i32.const 4))
)
;; check if next block is free -> merge them
(if (i32.eqz (call $hdr_get_free (local.get $next_hdr))) (then) (else
(local.set $size (i32.add (i32.add (local.get $size) (local.get $next_size)) (i32.const 8)))
(call $hdr_set_size (local.get $hdr) (local.get $size))
(call $hdr_set_size (local.get $next_ftr) (local.get $size))
))
))
)
;; copy a block of memory over, from src pointer to dst pointer
;; WebAssembly seems to be planning to support memory.copy
;; until then, this function uses a loop and i32.store8/load8
(func $memcpy (param $dst i32) (param $src i32) (param $n_bytes i32)
(local $ptr i32)
(local $offset i32)
(local $data i32)
(local.set $offset (i32.const 0))
loop $cpy
(local.set $data (i32.load8_u (i32.add (local.get $src) (local.get $offset))))
(i32.store8 (i32.add (local.get $dst) (local.get $offset)) (local.get $data))
(local.set $offset (i32.add (local.get $offset) (i32.const 1)))
(br_if $cpy (i32.lt_u (local.get $offset) (local.get $n_bytes)))
end
)
;; reallocate memory to new size
;; currently does not support contraction
;; nothing will happen if n_bytes is smaller than current payload size
(func $realloc (param $ptr i32) (param $n_bytes i32) (result i32)
(local $hdr i32)
(local $next_hdr i32)
(local $next_ftr i32)
(local $next_size i32)
(local $ftr i32)
(local $size i32)
(local $rest_hdr i32)
(local $rest_size i32)
(local $new_ptr i32)
(local.set $hdr (i32.sub (local.get $ptr) (i32.const 4)))
(local.set $size (call $hdr_get_size (local.get $hdr)))
(if (i32.gt_u (local.get $n_bytes) (local.get $size)) (then) (else
(local.get $ptr)
return
))
;; payload size is aligned to multiple of 4
(local.set $n_bytes (call $align4 (local.get $n_bytes)))
(local.set $next_hdr (i32.add (i32.add (local.get $hdr) (local.get $size)) (i32.const 8)))
;; Method I: try to expand the current block
;; check that we're not already the last block
(if (i32.lt_u (local.get $next_hdr) (global.get $max_addr) )(then
(if (call $hdr_get_free (local.get $next_hdr)) (then
(local.set $next_size (call $hdr_get_size (local.get $next_hdr)))
(local.set $rest_size (i32.sub
(local.get $next_size)
(i32.sub (local.get $n_bytes) (local.get $size))
))
(local.set $next_ftr (i32.add (i32.add (local.get $next_hdr) (local.get $next_size)) (i32.const 4)))
;; next block is big enough to be split into two
(if (i32.gt_s (local.get $rest_size) (i32.const 0) ) (then
(call $hdr_set_size (local.get $hdr) (local.get $n_bytes))
(local.set $ftr (i32.add (i32.add (local.get $hdr) (local.get $n_bytes) ) (i32.const 4)))
(call $hdr_set_size (local.get $ftr) (local.get $n_bytes))
(call $hdr_set_free (local.get $ftr) (i32.const 0))
(local.set $rest_hdr (i32.add (local.get $ftr) (i32.const 4) ))
(call $hdr_set_size (local.get $rest_hdr) (local.get $rest_size))
(call $hdr_set_free (local.get $rest_hdr) (i32.const 1))
(call $hdr_set_size (local.get $next_ftr) (local.get $rest_size))
(call $hdr_set_free (local.get $next_ftr) (i32.const 1))
(local.get $ptr)
return
;; next block is not big enough to be split, but is
;; big enough to merge with the current one into one
)(else (if (i32.gt_s (local.get $rest_size) (i32.const -9) ) (then
(local.set $size (i32.add (i32.add (local.get $size) (i32.const 8) ) (local.get $next_size)))
(call $hdr_set_size (local.get $hdr) (local.get $size))
(call $hdr_set_size (local.get $next_ftr) (local.get $size))
(call $hdr_set_free (local.get $next_ftr) (i32.const 0))
(local.get $ptr)
return
))))
))
))
;; Method II: allocate a new block and copy over
(local.set $new_ptr (call $malloc (local.get $n_bytes)))
(call $memcpy (local.get $new_ptr) (local.get $ptr) (local.get $n_bytes))
(call $free (local.get $ptr))
(local.get $new_ptr)
)
(func $memmove (param $dst i32) (param $src i32) (param $n_bytes i32)
(local $ptr i32)
(local $offset i32)
(local $data i32)
(if (i32.gt_u (local.get $dst) (local.get $src)) (then
(local.set $offset (i32.sub (local.get $n_bytes) (i32.const 1)))
loop $cpy_rev
(local.set $data (i32.load8_u (i32.add (local.get $src) (local.get $offset))))
(i32.store8 (i32.add (local.get $dst) (local.get $offset)) (local.get $data))
(local.set $offset (i32.sub (local.get $offset) (i32.const 1)))
(br_if $cpy_rev (i32.gt_s (local.get $offset) (i32.const -1)))
end
)(else
(local.set $offset (i32.const 0))
loop $cpy
(local.set $data (i32.load8_u (i32.add (local.get $src) (local.get $offset))))
(i32.store8 (i32.add (local.get $dst) (local.get $offset)) (local.get $data))
(local.set $offset (i32.add (local.get $offset) (i32.const 1)))
(br_if $cpy (i32.lt_u (local.get $offset) (local.get $n_bytes)))
end
))
)
;;------------------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ;;
;; ARRAY ;;
;; ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Continous, resizable storage for a sequence of values
;; struct arr {
;; void* data
;; int length
;; int elem_size
;; int capacity
;; }
;; (internal) getter/setters for arr struct fields
(func $_arr_set_data (param $ptr i32) (param $data i32)
(i32.store (local.get $ptr) (local.get $data))
)
(func $_arr_set_length (param $ptr i32) (param $length i32)
(i32.store (i32.add (local.get $ptr) (i32.const 4)) (local.get $length))
)
(func $_arr_set_elem_size (param $ptr i32) (param $elem_size i32)
(i32.store (i32.add (local.get $ptr) (i32.const 8)) (local.get $elem_size))
)
(func $_arr_set_capacity (param $ptr i32) (param $capacity i32)
(i32.store (i32.add (local.get $ptr) (i32.const 12)) (local.get $capacity))
)
(func $_arr_get_data (param $ptr i32) (result i32)
(i32.load (local.get $ptr))
)
(func $_arr_get_elem_size (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 8)))
)
(func $_arr_get_capacity (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 12)))
)
;; returns length of an array given an arr pointer
(func $arr_length (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 4)) )
)
;; initialize a new arr, returns a pointer to it
;; elem_size: size of each element, in bytes
(func $arr_new (param $elem_size i32) (result i32)
(local $ptr i32)
(local $data i32)
(local.set $ptr (call $malloc (i32.const 16)))
(local.set $data (call $malloc (i32.mul (global.get $DEFAULT_CAPACITY) (local.get $elem_size))))
(call $_arr_set_data (local.get $ptr) (local.get $data))
(call $_arr_set_length (local.get $ptr) (i32.const 0))
(call $_arr_set_elem_size (local.get $ptr) (local.get $elem_size))
(call $_arr_set_capacity (local.get $ptr) (global.get $DEFAULT_CAPACITY))
(local.get $ptr)
)
;; free allocated memory given an arr pointer
(func $arr_free (param $a i32)
(call $free (call $_arr_get_data (local.get $a)))
(call $free (local.get $a))
)
;; add an element to the end of the array
;; does not write the element, instead, returns a pointer
;; to the new last element for the user to write at
(func $arr_push (param $a i32) (result i32)
(local $length i32)
(local $capacity i32)
(local $data i32)
(local $elem_size i32)
(local.set $length (call $arr_length (local.get $a)))
(local.set $capacity (call $_arr_get_capacity (local.get $a)))
(local.set $data (call $_arr_get_data (local.get $a)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(if (i32.lt_u (local.get $length) (local.get $capacity) ) (then) (else
(local.set $capacity (i32.add
(i32.add (local.get $capacity) (i32.const 1))
(i32.mul (local.get $capacity) (i32.const 2))
))
(call $_arr_set_capacity (local.get $a) (local.get $capacity))
(local.set $data
(call $realloc (local.get $data) (i32.mul (local.get $elem_size) (local.get $capacity) ))
)
(call $_arr_set_data (local.get $a) (local.get $data))
))
(call $_arr_set_length (local.get $a) (i32.add (local.get $length) (i32.const 1)))
(i32.add (local.get $data) (i32.mul (local.get $length) (local.get $elem_size)))
)
;; returns a pointer to the ith element of an array
(func $arr_at (param $a i32) (param $i i32) (result i32)
(local $data i32)
(local $elem_size i32)
(local.set $data (call $_arr_get_data (local.get $a)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(i32.add (i32.mul (local.get $i) (local.get $elem_size)) (local.get $data))
)
;; remove the ith element of an array
(func $arr_remove (param $a i32) (param $i i32)
(local $data i32)
(local $elem_size i32)
(local $length i32)
(local $offset i32)
(local.set $length (call $arr_length (local.get $a)))
(local.set $data (call $_arr_get_data (local.get $a)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(local.set $offset
(i32.add (local.get $data) (i32.mul (local.get $i) (local.get $elem_size) ))
)
(call $memmove
(local.get $offset)
(i32.add (local.get $offset) (local.get $elem_size))
(i32.mul (i32.sub (local.get $length) (local.get $i) ) (local.get $elem_size))
)
(call $_arr_set_length (local.get $a) (i32.sub (local.get $length) (i32.const 1) ))
)
;; remove all elements in an array
(func $arr_clear (param $a i32)
(local $data i32)
(local $elem_size i32)
(local.set $data (call $_arr_get_data (local.get $a)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(call $free (local.get $data))
(call $_arr_set_data (local.get $a)
(call $malloc (i32.mul (local.get $elem_size) (global.get $DEFAULT_CAPACITY)))
)
(call $_arr_set_length (local.get $a) (i32.const 0))
(call $_arr_set_capacity (local.get $a) (global.get $DEFAULT_CAPACITY))
)
;; concatenate (join) two arrays
;; the first array will be extended in place, the second will be untouched
(func $arr_concat (param $a i32) (param $b i32)
(local $elem_size i32)
(local $a_data i32)
(local $a_length i32)
(local $a_capacity i32)
(local $b_data i32)
(local $b_length i32)
(local $b_capacity i32)
(local $sum_length i32)
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(local.set $a_length (call $arr_length (local.get $a)))
(local.set $a_data (call $_arr_get_data (local.get $a)))
(local.set $a_capacity (call $_arr_get_capacity (local.get $a)))
(local.set $b_length (call $arr_length (local.get $b)))
(local.set $b_data (call $_arr_get_data (local.get $b)))
(local.set $b_capacity (call $_arr_get_capacity (local.get $b)))
(local.set $sum_length (i32.add (local.get $a_length) (local.get $b_length)))
(if (i32.gt_u
(local.get $sum_length)
(local.get $a_capacity)
)(then
(local.set $a_capacity (local.get $sum_length))
(call $_arr_set_capacity (local.get $a) (local.get $a_capacity))
(local.set $a_data
(call $realloc (local.get $a_data) (i32.mul (local.get $elem_size) (local.get $a_capacity)))
)
(call $_arr_set_data (local.get $a) (local.get $a_data))
))
(call $memcpy
(i32.add (local.get $a_data) (i32.mul (local.get $a_length) (local.get $elem_size)))
(local.get $b_data)
(i32.mul (local.get $b_length) (local.get $elem_size))
)
(call $_arr_set_length (local.get $a) (local.get $sum_length))
)
;; insert into an array at given index
;; does not write the element, instead, returns a pointer
;; to the newly inserted slot for user to write at
(func $arr_insert (param $a i32) (param $i i32) (result i32)
(local $data i32)
(local $elem_size i32)
(local $length i32)
(local $offset i32)
(local.set $length (call $arr_length (local.get $a)))
(local.set $data (call $_arr_get_data (local.get $a)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(drop (call $arr_push (local.get $a)))
(local.set $offset
(i32.add (local.get $data) (i32.mul (local.get $i) (local.get $elem_size) ))
)
(call $memmove
(i32.add (local.get $offset) (local.get $elem_size))
(local.get $offset)
(i32.mul
(i32.sub (i32.sub (local.get $length) (i32.const 1)) (local.get $i) )
(local.get $elem_size)
)
)
(local.get $offset)
)
;; slice an array, producing a copy of a range of elements
;; i = starting index (inclusive), j = stopping index (exclusive)
;; returns pointer to new array
(func $arr_slice (param $a i32) (param $i i32) (param $j i32) (result i32)
(local $a_length i32)
(local $length i32)
(local $elem_size i32)
(local $ptr i32)
(local $data i32)
(local.set $a_length (call $arr_length (local.get $a)))
(if (i32.lt_s (local.get $i) (i32.const 0) )(then
(local.set $i (i32.add (local.get $a_length) (local.get $i)))
))
(if (i32.lt_s (local.get $j) (i32.const 0) )(then
(local.set $j (i32.add (local.get $a_length) (local.get $j)))
))
(local.set $length (i32.sub (local.get $j) (local.get $i)))
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(local.set $ptr (call $malloc (i32.const 16)))
(local.set $data (call $malloc (i32.mul (local.get $length) (local.get $elem_size))))
(call $_arr_set_data (local.get $ptr) (local.get $data))
(call $_arr_set_length (local.get $ptr) (local.get $length))
(call $_arr_set_elem_size (local.get $ptr) (local.get $elem_size))
(call $_arr_set_capacity (local.get $ptr) (local.get $length))
(call $memcpy (local.get $data)
(i32.add
(call $_arr_get_data (local.get $a))
(i32.mul (local.get $i) (local.get $elem_size))
)
(i32.mul (local.get $length) (local.get $elem_size))
)
(local.get $ptr)
)
;; reverse the order of elements in an array in-place
(func $arr_reverse (param $a i32)
(local $elem_size i32)
(local $tmp i32)
(local $stt i32)
(local $end i32)
(local.set $elem_size (call $_arr_get_elem_size (local.get $a)))
(local.set $stt (call $_arr_get_data (local.get $a)))
(local.set $end (i32.add
(local.get $stt)
(i32.mul (local.get $elem_size) (i32.sub (call $arr_length (local.get $a)) (i32.const 1) ))
))
(local.set $tmp (call $malloc (local.get $elem_size)))
loop $loop_arr_rev
(if (i32.lt_u (local.get $stt) (local.get $end)) (then
(call $memcpy (local.get $tmp) (local.get $stt) (local.get $elem_size))
(call $memcpy (local.get $stt) (local.get $end) (local.get $elem_size))
(call $memcpy (local.get $end) (local.get $tmp) (local.get $elem_size))
(local.set $stt (i32.add (local.get $stt) (local.get $elem_size)))
(local.set $end (i32.sub (local.get $end) (local.get $elem_size)))
(br $loop_arr_rev)
))
end
(call $free (local.get $tmp))
)
;;------------------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ;;
;; LIST ;;
;; ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Doubly linked list
;; For users, list element operation always deal with pointers to the
;; actual data portion of each node, so they can conveniently write at the
;; pointer returned. Internally however, the data field is preceded
;; by `prev` and `next` fields, and node pointers points to the beginning of
;; the node struct instead. Upon returning values to the user, the offset is
;; added.
;; struct list {
;; listnode* head
;; listnode* tail
;; int length
;; int elem_size
;; }
;; (internal) getters and setters for list struct fields
(func $_list_set_head (param $ptr i32) (param $head i32)
(i32.store (local.get $ptr) (local.get $head))
)
(func $_list_set_tail (param $ptr i32) (param $tail i32)
(i32.store (i32.add (local.get $ptr) (i32.const 4)) (local.get $tail))
)
(func $_list_set_length (param $ptr i32) (param $length i32)
(i32.store (i32.add (local.get $ptr) (i32.const 8)) (local.get $length))
)
(func $_list_set_elem_size (param $ptr i32) (param $elem_size i32)
(i32.store (i32.add (local.get $ptr) (i32.const 12)) (local.get $elem_size))
)
(func $_list_get_head (param $ptr i32) (result i32)
(i32.load (local.get $ptr))
)
(func $_list_get_tail (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 4)) )
)
(func $list_length (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 8)) )
)
(func $_list_get_elem_size (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 12)))
)
;; get pointer to head of the list
(func $list_head (param $ptr i32) (result i32)
(local.set $ptr (i32.load (local.get $ptr)))
(if (i32.eqz (local.get $ptr))(then
(i32.const 0)
return
))
(i32.add (local.get $ptr) (i32.const 8))
)
;; get pointer to tail of the list
(func $list_tail (param $ptr i32) (result i32)
(local.set $ptr (i32.load (i32.add (local.get $ptr) (i32.const 4)) ))
(if (i32.eqz (local.get $ptr))(then
(i32.const 0)
return
))
(i32.add (local.get $ptr) (i32.const 8))
)
;; struct listnode {
;; listnode* prev
;; listnode* next
;; data_t data
;; }
;; (internal) getters and setters for list struct fields
(func $_listnode_set_prev (param $ptr i32) (param $prev i32)
(i32.store (local.get $ptr) (local.get $prev))
)
(func $_listnode_set_next (param $ptr i32) (param $next i32)
(i32.store (i32.add (local.get $ptr) (i32.const 4)) (local.get $next))
)
(func $_listnode_get_prev (param $ptr i32) (result i32)
(i32.load (local.get $ptr))
)
(func $_listnode_get_next (param $ptr i32) (result i32)
(i32.load (i32.add (local.get $ptr) (i32.const 4)) )
)
;; get pointer to previous element
(func $list_prev (param $ptr i32) (result i32)
(local.set $ptr (i32.load (i32.sub (local.get $ptr) (i32.const 8))))
(if (i32.eqz (local.get $ptr))(then
(i32.const 0)
return
))
(i32.add (local.get $ptr) (i32.const 8))
)
;; get pointer to next element
(func $list_next (param $ptr i32) (result i32)
(local.set $ptr (i32.load (i32.sub (local.get $ptr) (i32.const 4))))
(if (i32.eqz (local.get $ptr))(then
(i32.const 0)
return
))
(i32.add (local.get $ptr) (i32.const 8))
)
;; initializes a new list, and returns a pointer to it
;; elem_size: size of each element, in bytes
(func $list_new (param $elem_size i32) (result i32)
(local $ptr i32)
(local.set $ptr (call $malloc (i32.const 16)))
(call $_list_set_head (local.get $ptr) (i32.const 0))
(call $_list_set_tail (local.get $ptr) (i32.const 0))
(call $_list_set_elem_size (local.get $ptr) (local.get $elem_size))
(call $_list_set_length (local.get $ptr) (i32.const 0))
(local.get $ptr)
)
;; add an element to the end of the list
;; does not write the element, instead, returns a pointer
;; to the new last element for the user to write at
(func $list_push (param $l i32) (result i32)
(local $node i32)
(local $tail i32)