-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrt.wat
More file actions
1459 lines (1287 loc) · 44.2 KB
/
Copy pathrt.wat
File metadata and controls
1459 lines (1287 loc) · 44.2 KB
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
(module
;; Imported functions from the host
{{USER_IMPORTS}}
;; Function reference table
{{USER_TABLE}}
;; Memory export
(memory (export "__memory") {{PAGES_NEEDED}})
;; Reference stack pointer
(global $__ref_sp (mut i32) (i32.const {{STACK_END}}))
;; Reference variables stack pointer
(global $__rv_sp (mut i32) (i32.const {{RV_STACK_END}}))
;; Push a pointer onto the reference stack
(func $__ref_stack_push (param $ptr i32)
;; Note decrement for better cache efficiency
;; __ref_sp--
global.get $__ref_sp
i32.const 4
i32.sub
global.set $__ref_sp
;; *__ref_sp = ptr
global.get $__ref_sp
local.get $ptr
i32.store
)
;; Pop a pointer from the top of the reference stack
(func $__ref_stack_pop (result i32)
;; ret = *__ref_sp
global.get $__ref_sp
i32.load
;; __ref_sp++
global.get $__ref_sp
i32.const 4
i32.add
global.set $__ref_sp
;; return ret
)
;; Instead functions allocate chunks of known sizes based on needs
;; ;; Store a reference on the rv stack
;; (func $__rv_stack_push (result i32)
;; ;; return *(--__rv_sp) = ref_stack_pop()
;; global.get $__rv_sp
;; i32.const 4
;; i32.sub
;; global.set $__rv_sp
;; global.get $__rv_sp
;; call $__ref_stack_pop
;; i32.store
;; global.get $__ref_sp
;; )
;; ;; Pop N references from the rv stack
;; (func $__rv_stack_pop (param $n i32)
;; ;; __rv_sp += 4*n
;; global.get $__rv_sp
;; local.get $n
;; i32.const 2
;; i32.shl
;; i32.add
;; global.set $__rv_sp
;; )
;; Initialize static data
(data (i32.const {{STACK_SIZE}}) "{{STATIC_DATA_STR}}")
;; Initialize nursery head
;; (data (i32.const {{NURSERY_SP_INIT}}) "\00\00\00\00" "\00\00\00\00" "\00\00\00\00")
;; Nursery stack pointer
(global $__nursery_sp (mut i32) (i32.const {{NURSERY_SP_INIT}}))
;; Does the given pointer fall within the region of the nursery?
(func $__in_nursery (param $ptr i32) (result i32)
;; OPTIMIZATION single compare, don't need to worry about lhs check
local.get $ptr
i32.const {{NURSERY_START}}
i32.ge_u
local.get $ptr
i32.const {{NURSERY_END}}
i32.lt_u
i32.and
)
;; Heap start
(global $__heap_start i32
(i32.const {{HEAP_START}}))
;; Initialize heap head
;; (data
;; (i32.const {{HEAP_START}})
;; "\00\00\00\00" "\00\00\00\00" "\00\00\00\00")
;; Initialize last item stored on the heap
(global $__heap_tail (mut i32) (i32.const {{HEAP_START}}))
;; Initialize last free space
(global $__free_head (mut i32) (i32.const {{FREE_START}}))
(data (i32.const {{FREE_START}}) "{{INIT_FREE_SIZE_STR}}")
;; Allocate an object (w/ preference to nursery)
;; Note that size is measured in multiples of 32 bits
(func $__alloc (param $size i32) (param $ref_bitfield_addr i32) (result i32)
(local $new_nsp i32)
;; Check if we can fit it into the nursery
;; nursery_sp - (size + sizeof(obj_header)) < START_OF_NURSERY
global.get $__nursery_sp ;; last allocated object start
i32.const 12 ;; size of heap object header (3 x i32)
local.get $size
i32.const 2
i32.shl
i32.add
i32.sub
local.tee $new_nsp
i32.const {{NURSERY_START}} ;; start of nursery
i32.lt_u
if
;; Item too big to fit into an empty nursery
local.get $size
i32.const {{NURSERY_SIZE}}
i32.const 1
i32.shr_u ;; 50% of nursery => put it in the heap
i32.gt_u
if
;; Allocate it onto the heap, not the nursery
(return (call $__alloc_heap
(local.get $size)
(local.get $ref_bitfield_addr)))
else
;; Empty the nursery to make room
call $__do_gc
local.get $size
local.get $ref_bitfield_addr
call $__alloc
return
end
end
;; Else: Perform allocation
;; nursery_sp->next = updated_nursery_sp
global.get $__nursery_sp
local.get $new_nsp
i32.store offset=8
;; Update nsp
local.get $new_nsp
global.set $__nursery_sp
;; Initialize new header
;; nsp->bitfield = ref_bitfield_addr
local.get $new_nsp
local.get $ref_bitfield_addr
i32.store
;; nsp->size = size
local.get $new_nsp
local.get $size
i32.store offset=4
;; nsp->next = NULL
local.get $new_nsp
i32.const 0
i32.store offset=8
;; Return the start of the payload
;; return (nsp + sizeof(header))
local.get $new_nsp
i32.const 12
i32.add
)
;; Marks user-provided memory address
;; Note this means user cannot have objects stored in static memory
(func $__mark (param $user_ptr i32)
;; Pointer to the start of header object for $user_ptr
(local $m_ptr i32)
;; Value of the mark member of header object
(local $m_mark_size i32) ;; recycled: localized index of bit
;; Address of the references bitfield
(local $m_bf_addr i32)
;; Locals for iterating over the references bitfield
;; Note: Initialized to 0
(local $bit_ind i32) ;; Current Bit in the references bitfield
(local $bf_cursor i64) ;; Scanned 64bit section of ref bitfield
(local $local_ind i32) ;; Index within the Scanned 64bit section
;; check nullptr
local.get $user_ptr
i32.eqz
if
return
end
;; Get mark
local.get $user_ptr
i32.const 12
i32.sub
local.tee $m_ptr
i32.load offset=4
local.set $m_mark_size
;; If we've already visited, return
local.get $m_mark_size
i32.const 0xc0000000
i32.and
if
return
end
;; Write mark
local.get $m_ptr
local.get $m_mark_size
i32.const 0xc0000000
i32.or
i32.store offset=4
;; Recursively iterate through bitfield references
;; Read bf addr
local.get $m_ptr
i32.load
local.tee $m_bf_addr
i32.eqz
if ;; No references (optimization)
return
end
loop $for_each_bit
;; If it's the first bit in an i64
local.get $bit_ind
i32.const 64
i32.rem_u
local.tee $local_ind
i32.eqz
if
;; Load the next 64 bits from the bitfield
local.get $bit_ind
i32.const 3
i32.shr_u ;; 64 / 8 *
local.get $m_bf_addr
i32.add
i64.load
local.set $bf_cursor
end
;; If bit indicates a reference
i64.const 0x1
local.get $local_ind
i32.const 7
i32.xor ;; wasm is little endian
i64.extend_i32_u
i64.shl
local.get $bf_cursor
i64.and
i64.eqz
i32.eqz ;; convert i64 to boolean -> !!
if
;; Mark referenced pointer
local.get $bit_ind
i32.const 2
i32.shl
local.get $user_ptr
i32.add
i32.load
call $__mark ;; this could overflow the stack
end
;; Do while ++bit_ind < size
;; Remember that size is denoted as multiples of 32 bits
;; Note that the mark part in the local is always 0b00 (see: return)
local.get $bit_ind
i32.const 1
i32.add
local.tee $bit_ind
local.get $m_mark_size
i32.lt_u
br_if $for_each_bit
end
)
;; Identical to mark except it ignores pointers which aren't in the nursery
(func $__minor_mark (param $user_ptr i32)
;; Pointer to the start of header object for $user_ptr
(local $m_ptr i32)
;; Value of the mark member of header object
(local $m_mark_size i32) ;; recycled: localized index of bit
;; Address of the references bitfield
(local $m_bf_addr i32)
;; Locals for iterating over the references bitfield
;; Note: Initialized to 0
(local $bit_ind i32) ;; Current Bit in the references bitfield
(local $bf_cursor i64) ;; Scanned 64bit section of ref bitfield
(local $local_ind i32) ;; Index within the Scanned 64bit sectio
;; check nullptr
local.get $user_ptr
i32.eqz
if
return
end
;; Skip pointers not in nursery
local.get $user_ptr
call $__in_nursery
i32.eqz
if
return
end
;; Get mark
local.get $user_ptr
i32.const 12
i32.sub
local.tee $m_ptr
i32.load offset=4
local.set $m_mark_size
;; If we've already visited, return
local.get $m_mark_size
i32.const 0xc0000000
i32.and
if
return
end
;; Write mark
local.get $m_ptr
local.get $m_mark_size
i32.const 0xc0000000
i32.or
i32.store offset=4
;; Recursively iterate through bitfield references
;; Read bf addr
local.get $m_ptr
i32.load
local.tee $m_bf_addr
i32.eqz
if ;; No references (optimization)
return
end
loop $for_each_bit
;; If it's the first bit in an i64
local.get $bit_ind
i32.const 64
i32.rem_u
local.tee $local_ind
i32.eqz
if
;; Load the next 64 bits from the bitfield
local.get $bit_ind
i32.const 3
i32.shr_u ;; 64 / 8 *
local.get $m_bf_addr
i32.add
i64.load
local.set $bf_cursor
end
;; If bit indicates a reference
i64.const 0x1
local.get $local_ind
i32.const 7
i32.xor ;; wasm is little endian
i64.extend_i32_u
i64.shl
local.get $bf_cursor
i64.and
i64.eqz
i32.eqz ;; convert i64 to boolean -> !!
if
;; Mark referenced pointer (DFS)
local.get $bit_ind
i32.const 2
i32.shl ;; * sizeof(i32)
local.get $user_ptr
i32.add
i32.load
call $__minor_mark ;; FIXME this overflows the stack
end
;; Do while ++bit_ind < size
;; Remember that size is denoted as multiples of 32 bits
;; Note that the mark part in the local is always 0b00 (see: return)
local.get $bit_ind
i32.const 1
i32.add
local.tee $bit_ind
local.get $m_mark_size
i32.le_u
br_if $for_each_bit
end
)
;; Allocate an object onto the heap
;; Note mark_size also copies the given mark
;; Note that size is measured in multiples of 32 bits
(func $__alloc_heap (param $mark_size i32) (param $bf_ptr i32) (result i32)
(local $free_p i32) ;; current gc_heap_empty_t*
(local $f_size i32) ;; value of size field in free_p
(local $f_next i32) ;; pointer to next freespace in free_p
(local $last_free_p i32) ;; previous value of free_p or 0 if first
(local $delta i32) ;; multipurpose, difference
(local $next i32) ;; copy next pointer before overwriting it
(local $just_size i32) ;; Size without the mark + size of object header
;; Align to nearest 64 bits (obj header is 96)
local.get $mark_size
i32.const 0x1
i32.or
i32.const 0x3fffffff ;; ignore mark bitfield component
i32.and
local.tee $mark_size
;; add size of header
i32.const 3
i32.add
local.set $just_size
;; Start at start of free-list
global.get $__free_head
;; local.tee $last_free_p
local.set $free_p
loop $next_empty
;; (free_v = *free_ptr).size >= just_size
;; read freespace header
local.get $free_p
i32.load
local.set $f_size
local.get $free_p
i32.load offset=4
local.set $f_next
;; Check if it fits
local.get $just_size
local.get $f_size
i32.ge_u
if ;; Too big for this free space
;; if (free_p->next == NULL)
local.get $f_next
if ;; check to next node
;; last_free_p = free_p
local.get $free_p
local.set $last_free_p
;; free_p = free_p->next
local.get $f_next
local.set $free_p
;; maybe this one is big enough
br $next_empty
end
;; Else: this is the last node
;; Expand linear memory to fill the gap
local.get $just_size
local.get $f_size
i32.sub
i32.const 14
i32.shr_u ;; / ((1024 B/KiB * 64 KiB/page) / 4 B/i32)
i32.const 1
i32.add ;; guarantee there's always some free space at the end
local.tee $delta
memory.grow
i32.const -1
i32.eq
if
unreachable ;; failed to get more memory -> panic
end
;; delta now stores the size of the free space that will remain
;; after the object is allocated
local.get $delta
i32.const 14
i32.shl ;; pages -> # i32's
local.get $f_size
i32.add
local.get $just_size
i32.sub
local.set $delta
;; Overwrite free object with our object header
local.get $free_p
local.get $bf_ptr
i32.store
local.get $free_p
local.get $mark_size
i32.store offset=4
local.get $free_p
i32.const 0
i32.store offset=8
;; Update heap tail
global.get $__heap_tail
local.get $free_p
i32.store offset=8
local.get $free_p
global.set $__heap_tail
;; Make new excess space new free object in list
;; *(free_p = free_p + just_size) = gc_heap_empty_t { .size=delta, .next=NULL }
local.get $free_p
local.get $just_size
i32.const 2
i32.shl ;; * sizeof(i32)
i32.add
local.tee $free_p
local.get $delta
i32.store
local.get $free_p
i32.const 0
i32.store offset=4
;; Update last free item entry
local.get $last_free_p
if
;; new end of list
local.get $last_free_p
local.get $free_p
i32.store offset=4
else
;; new start of list
local.get $free_p
global.set $__free_head
end
global.get $__heap_tail
i32.const 12
i32.add
return
else ;; Small enough for this free space
;; Store leftover space into delta
;; delta = (free_size - just_size)
local.get $f_size
local.get $just_size
i32.sub
local.set $delta
;; Copy next pointer
;; next = free_p->next
local.get $free_p
i32.load offset=4
local.set $next
;; Overwrite free object with our object header
;; free_p now refers to heap_object_header_t*
;; free_p->refs_bitfield_addr = bf_addr
;; free_p->mark_size = mark_size
;; free_p->next = 0
local.get $free_p
local.get $bf_ptr
i32.store
local.get $free_p
local.get $mark_size
i32.store offset=4
local.get $free_p
i32.const 0
i32.store offset=8
;; Add object to LL
;; heap_tail->next = free_p
;; heap_tail = free_p
global.get $__heap_tail
local.get $free_p
i32.store offset=8
local.get $free_p
global.set $__heap_tail
;; Check leftover space
local.get $delta
if
;; leftover space
;; Write new free space head
;; (free_p = free_p + just_size * sizeof(i32))->size = delta
local.get $free_p
local.get $just_size
i32.const 2
i32.shl ;; * sizeof(i32)
i32.add
local.tee $free_p
local.get $delta
i32.store
;; free_p->next = next
local.get $free_p
local.get $next
i32.store offset=4
;; Maintain LL
local.get $last_free_p
if
local.get $last_free_p
local.get $free_p
i32.store offset=4
else
local.get $free_p
global.set $__free_head
end
else
;; no leftover space
local.get $last_free_p
if ;; Make previous freespace new free list head
local.get $last_free_p
local.get $next
i32.store offset=4
else ;; We have exactly populated the heap
;; Grow memory to make a new freespace
;; Put the new free head at end of lm
;; TODO calculate this via free_p instead?
memory.size
i32.const 16
i32.shl ;; * bytes/page
global.set $__free_head
;; Extend lm by a page (64 KiB)
i32.const 1
memory.grow
i32.const -1
i32.eq
if
unreachable ;; panic: failed
end
;; Store the size of new free space
global.get $__free_head
i32.const 16384 ;; 1024 B/KiB * 64KiB/page / 4 B/I32
i32.store
end
end
global.get $__heap_tail
i32.const 12
i32.add
return
end
end
unreachable
)
;; Similar to memcpy but works in multiples of 32 bits for improved performance
;; note that len is in multiples of 32 bits
;; could maybe optimize by using a 64 bit read head but eh
(func $memcpy32 (param $dest i32) (param $src i32) (param $len i32)
local.get $len
if
;; len = len * 4 + dest
local.get $dest
local.get $len
i32.const 2
i32.shl
i32.add
local.set $len
loop $cp_loop
;; *dest = *src
local.get $dest
local.get $src
i32.load
i32.store
;; src++; dest++
local.get $src
i32.const 4
i32.add
local.set $src
local.get $dest
i32.const 4
i32.add
local.tee $dest
;; Do while dest < len
local.get $len
i32.lt_u
br_if $cp_loop
end $cp_loop
end
)
;; increments each time we do gc
(global $__gc_cycle (mut i32) (i32.const 0))
;; Collect Garbage
(; TODO lots of room for optimization for minor gc/nursery
- don't need next ptr
;)
(func $__do_gc
(local $p i32) ;; stack pointer
(local $dest i32) ;; pointer to where object is being moved
(local $is_major i32) ;; is this gc a major gc?
(local $mark_size i32) ;; mark+size fields
(local $bf i32) ;; bitfield addres
;; Mark
;; p = end of reference stack
global.get $__ref_sp
local.tee $p
;; If there's nothing to mark, delete everything
;; if p == reference stack end && rv_sp == ref vars stack end
i32.const {{STACK_SIZE}}
i32.eq
global.get $__rv_sp
i32.const {{RV_STACK_END}}
i32.eq
i32.and
if ;; stack is empty -> everything is garbage
;; Reset globals
i32.const {{NURSERY_SP_INIT}}
global.set $__nursery_sp
i32.const {{HEAP_START}}
global.set $__heap_tail
i32.const {{FREE_START}}
global.set $__free_head
;; heap_start->next = null
i32.const {{HEAP_START}}
i32.const 0
i32.store offset=8
;; free_start->size = (free_start - (memory.size * PAGE_SIZE)) / sizeof(i32)
i32.const {{FREE_START}}
memory.size
i32.const 16
i32.shl
i32.const {{FREE_START}}
i32.sub
i32.const 2
i32.shr_u
i32.store
;; free_start->next = null
i32.const {{FREE_START}}
i32.const 0
i32.store offset=4
return
end
;; is_major = (++gc_cycle) % GEN_RATIO == 0
global.get $__gc_cycle
i32.const 1
i32.add
global.set $__gc_cycle
global.get $__gc_cycle
i32.const 4 ;; TODO tune this
i32.rem_u
i32.eqz
local.tee $is_major
;; for each pointer on the references stack
if
loop $mark_loop
;; mark(*p)
local.get $p
i32.load
call $__mark
;; do while (++p < end_of_stack)
local.get $p
i32.const 4
i32.add
local.tee $p
i32.const {{STACK_END}}
i32.lt_u
br_if $mark_loop
end $mark_loop
else
loop $mark_loop
;; mark(*p)
local.get $p
i32.load
call $__minor_mark
;; do while (++p < end_of_stack)
local.get $p
i32.const 4
i32.add
local.tee $p
i32.const {{STACK_END}}
i32.lt_u
br_if $mark_loop
end $mark_loop
end
;; p = ref var stack pointer
global.get $__rv_sp
local.set $p
;; for each pointer on the ref var stack
local.get $is_major
if
loop $mark_loop2
;; mark(*p)
local.get $p
i32.load
call $__mark
;; do while (++p < end_of_stack)
local.get $p
i32.const 4
i32.add
local.tee $p
i32.const {{RV_STACK_END}}
i32.lt_u
br_if $mark_loop2
end $mark_loop2
else
loop $mark_loop2
;; mark(*p)
local.get $p
i32.load
call $__minor_mark
;; do while (++p < end_of_stack)
local.get $p
i32.const 4
i32.add
local.tee $p
i32.const {{RV_STACK_END}}
i32.lt_u
br_if $mark_loop2
end $mark_loop2
end
;; If major gc, sweep the heap before emptying the nursery
local.get $is_major
if
global.get $__heap_start
local.tee $p
;; Note that the head object does not get freed and always has size zero
i32.const 0xc0000000
i32.store offset=4
;; $dest is used as $prev in this branch
;; it's initialized to null
;; for each object in the heap
loop $sweep
;; if unmarked
local.get $p
i32.load offset=4
local.tee $mark_size
i32.const 0xc0000000
i32.and
i32.eqz
if
;; Free it
local.get $p
local.get $dest
call $__heap_free
;; Go next
local.get $dest
i32.load offset=8
local.tee $p
br_if $sweep
else
;; Remove mark
local.get $p
local.get $mark_size
i32.const 0x3fffffff
i32.and
i32.store offset=4
end
;; Do while ((p = (dest = p)->next) != 0)
local.get $p
local.tee $dest
i32.load offset=8
local.tee $p
br_if $sweep
end $sweep
;; Coalese adjacent free spaces
call $__coalesce
end
;; Move marked stuff from nursery to the main heap
;; Iterate over items in the nursery
global.get $__nursery_sp
local.set $p
loop $cp_loop
;; Load header
local.get $p
i32.load
local.set $bf
local.get $p
i32.load offset=4
local.tee $mark_size
;; If marked
i32.const 0xc0000000
i32.and
if ;; Copy it into the main heap
;; Allocate space on heap
local.get $mark_size
i32.const 0x3fffffff
i32.and
local.get $bf
call $__alloc_heap
local.set $dest
;; Store address into the next pointer field
local.get $p
local.get $dest
i32.store offset=8
;; memcpy user data
local.get $dest
local.get $p
i32.const 12
i32.add
local.get $mark_size
i32.const 0x3fffffff
i32.and
call $memcpy32
end
;; if (p = p + sizeof(obj_header_t) + p->size) < END_OF_NURSERY - sizeof(obj_header_t)
local.get $p
i32.const 12
i32.add
local.get $mark_size
i32.const 0x3fffffff
i32.and
i32.const 2
i32.shl
i32.add
local.tee $p
i32.const {{NURSERY_SP_INIT}}
i32.lt_u
br_if $cp_loop
end $cp_loop
;; Update references
;; Update stack refs
;; for each pointer on the references stack
global.get $__ref_sp
local.set $p
loop $rsu_loop
local.get $p
i32.load
local.tee $dest
call $__in_nursery
if
;; Update reference to new location
local.get $p
local.get $dest
i32.const 4
i32.sub
i32.load
i32.store
end
;; do while (++p < end_of_stack)
local.get $p
i32.const 4
i32.add
local.tee $p
i32.const {{STACK_SIZE}}
i32.lt_u
br_if $rsu_loop
end $rsu_loop
;; Update references to objs previously stored in nursery
call $__update_nursery_refs