-
Notifications
You must be signed in to change notification settings - Fork 2
/
rt_gen_gc.s
1994 lines (1790 loc) · 92.7 KB
/
rt_gen_gc.s
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
########################################
# Read Only Data
########################################
.section .rodata
########################################
# Messages for the GenGC garbage collector
########################################
.GenGC.MSG_INIT_OK_ASCII: .ascii "GenGC: Initialized"
.GenGC.MSG_INIT_OK_LEN = (. - .GenGC.MSG_INIT_OK_ASCII)
.GenGC.MSG_INITED_IN_TEST_ASCII: .ascii "GenGC: Initialized in test mode"
.GenGC.MSG_INITED_IN_TEST_LEN = (. - .GenGC.MSG_INITED_IN_TEST_ASCII)
.GenGC.MSG_INIT_ERROR_ASCII: .ascii "GenGC: Unable to initialize"
.GenGC.MSG_INIT_ERROR_LEN = (. - .GenGC.MSG_INIT_ERROR_ASCII)
.GenGC.MSG_COLLECTING_ASCII: .ascii "GenGC: Collecting"
.GenGC.MSG_COLLECTING_LEN = (. - .GenGC.MSG_COLLECTING_ASCII)
.GenGC.MSG_MAJOR_ASCII: .ascii "GenGC: Major collection"
.GenGC.MSG_MAJOR_LEN = (. - .GenGC.MSG_MAJOR_ASCII)
.GenGC.MSG_MAJOR_ERROR_ASCII: .ascii "GenGC: Fatal error during a major garbage collection"
.GenGC.MSG_MAJOR_ERROR_LEN = (. - .GenGC.MSG_MAJOR_ERROR_ASCII)
.GenGC.MSG_MINOR_ASCII: .ascii "GenGC: Minor collection"
.GenGC.MSG_MINOR_LEN = (. - .GenGC.MSG_MINOR_ASCII)
.GenGC.MSG_MINOR_ERROR_ASCII: .ascii "GenGC: Fatal error during a minor garbage collection"
.GenGC.MSG_MINOR_ERROR_LEN = (. - .GenGC.MSG_MINOR_ERROR_ASCII)
.GenGC.MSG_HEAP_START_EQ_ASCII: .ascii "GenGC: HEAP START = "
.GenGC.MSG_HEAP_START_EQ_LEN = (. - .GenGC.MSG_HEAP_START_EQ_ASCII)
.GenGC.MSG_HEAP_END_EQ_ASCII: .ascii "GenGC: HEAP END = "
.GenGC.MSG_HEAP_END_EQ_LEN = (. - .GenGC.MSG_HEAP_END_EQ_ASCII)
.GenGC.MSG_HDR_L0_EQ_ASCII: .ascii "GenGC: L0 = "
.GenGC.MSG_HDR_L0_EQ_LEN = (. - .GenGC.MSG_HDR_L0_EQ_ASCII)
.GenGC.MSG_HDR_L1_EQ_ASCII: .ascii "GenGC: L1 = "
.GenGC.MSG_HDR_L1_EQ_LEN = (. - .GenGC.MSG_HDR_L1_EQ_ASCII)
.GenGC.MSG_HDR_L2_EQ_ASCII: .ascii "GenGC: L2 = "
.GenGC.MSG_HDR_L2_EQ_LEN = (. - .GenGC.MSG_HDR_L2_EQ_ASCII)
.GenGC.MSG_HDR_L3_EQ_ASCII: .ascii "GenGC: L3 = "
.GenGC.MSG_HDR_L3_EQ_LEN = (. - .GenGC.MSG_HDR_L3_EQ_ASCII)
.GenGC.MSG_HDR_L4_EQ_ASCII: .ascii "GenGC: L4 = "
.GenGC.MSG_HDR_L4_EQ_LEN = (. - .GenGC.MSG_HDR_L4_EQ_ASCII)
.GenGC.MSG_HDR_MINOR0_EQ_ASCII: .ascii "GenGC: MINOR0 = "
.GenGC.MSG_HDR_MINOR0_EQ_LEN = (. - .GenGC.MSG_HDR_MINOR0_EQ_ASCII)
.GenGC.MSG_HDR_MINOR1_EQ_ASCII: .ascii "GenGC: MINOR1 = "
.GenGC.MSG_HDR_MINOR1_EQ_LEN = (. - .GenGC.MSG_HDR_MINOR1_EQ_ASCII)
.GenGC.MSG_HDR_MAJOR0_EQ_ASCII: .ascii "GenGC: MAJOR0 = "
.GenGC.MSG_HDR_MAJOR0_EQ_LEN = (. - .GenGC.MSG_HDR_MAJOR0_EQ_ASCII)
.GenGC.MSG_HDR_MAJOR1_EQ_ASCII: .ascii "GenGC: MAJOR1 = "
.GenGC.MSG_HDR_MAJOR1_EQ_LEN = (. - .GenGC.MSG_HDR_MAJOR1_EQ_ASCII)
.GenGC.MSG_HDR_STACK_BASE_EQ_ASCII: .ascii "GenGC: STACK BASE = "
.GenGC.MSG_HDR_STACK_BASE_EQ_LEN = (. - .GenGC.MSG_HDR_STACK_BASE_EQ_ASCII)
.GenGC.MSG_ALLOC_PTR_EQ_ASCII: .ascii "GenGC: ALLOC PTR = "
.GenGC.MSG_ALLOC_PTR_EQ_LEN = (. - .GenGC.MSG_ALLOC_PTR_EQ_ASCII)
.GenGC.MSG_ALLOC_LIMIT_EQ_ASCII: .ascii "GenGC: ALLOC LIMIT = "
.GenGC.MSG_ALLOC_LIMIT_EQ_LEN = (. - .GenGC.MSG_ALLOC_LIMIT_EQ_ASCII)
########################################
# Text
########################################
# The following memory management and garbage collection code has been
# adapted from [the Cool runtime system](https://theory.stanford.edu/~aiken/software/cooldist/lib/trap.handler).
#
# The source code in trap.handler is covered by the following copyright notice.
#
# Copyright (c) 1995,1996 The Regents of the University of California.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software
# for any purpose, without fee, and without written agreement is
# hereby granted, provided that the above copyright notice and the following
# two paragraphs appear in all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
.text
.include "constants.inc"
########################################
# GenGC Generational Garbage Collector
#
# This is an implementation of a generational garbage collector
# as described in "Simple Generational Garbage Collection and Fast
# Allocation" by Andrew W. Appel [Princeton University, March 1988].
# This is a two generation scheme which uses an assignment table
# to handle root pointers located in the older generation objects.
#
# When the work area is filled, a minor garbage collection takes place
# which moves all live objects into the reserve area. These objects
# are then incorporated into the old area. New reserve and work areas
# are setup and allocation can continue in the work area. If a break-
# point is reached in the size of the old area just after a minor
# collection, a major collection then takes place. All live objects in
# the old area are then copied into the new area, expanding the heap if
# necessary. The X and new areas are then block copied back L1-L0
# bytes to form the next old area.
#
# The assignment table is implemented as a stack growing towards the
# allocation pointer (`.Alloc.ptr`) in the work area. If they cross, a minor
# collection is then carried out. This allows the garbage collector
# not to have to keep a fixed table of assignments. As a result, programs
# with many assignments will tend not to be bogged down with extra
# garbage collections [to make space in a fixed-size assignment table?].
#
# The unused area was implemented to help keep the garbage collector
# from continually expanding the heap. This buffer zone allows major
# garbage collections to happen earlier, reducing the risk of expansions
# due to too many live objects in the old area. The histories kept by
# the garbage collector in MAJOR0, MAJOR1, MINOR0, and MINOR1 also help
# to prevent unnecessary expansions of the heap. If many live objects
# were recently collected, the garbage collections will start to occur
# sooner.
#
# Note that during a minor collection, the work area is guaranteed to
# fit within the reserve area. However, during a major collection, the
# old area will not necessarily fit in the new area. If the latter occurs,
# `.GenGC.offset_copy` will detect this and expand the heap.
#
# The heap is expanded on two different occasions:
#
# 1) After a major collection, the old area is set to be at most
# 1/(2^GenGC_OLDRATIO) of the usable heap (L0 to L3). Note that
# first L4 is checked to see if any of the unused memory between L3
# and L4 is enough to satisfy this requirement. If not, then the
# heap will be expanded. If it is, the appropriate amount will be
# transfered from the unused area to the work/reserve area.
#
# 2) During a major collection, if the live objects in the old area
# do not fit within the new area, the heap is expanded and `.Alloc.limit`
# is updated to reflect this. This value later gets stored back
# into L4.
#
# During a normal allocation and minor collections, the heap has the
# following form:
#
# Header
# |
# | Older generation objects
# | |
# | | Minor garbage collection area
# | | |
# | | | Allocation area
# | | | |
# | | | | Assignment table
# | | | | |
# | | | | | Unused
# | | | | | |
# v v v v v v
# +----+--------------+-----------------+-------------+---+---------+
# |XXXX| Old Area | Reserve Area | Work Area |XXX| Unused |
# +----+--------------+-----------------+-------------+---+---------+
# ^ ^ ^ ^ ^ ^ ^ ^
# | | | | |--> <--| | |
# | L0 L1 L2 | | L3 L4
# | | |
# heap_start .Alloc.ptr .Alloc.limit
#
# `.Alloc.ptr`: points to the next free word in the work
# area during normal allocation. During a minor garbage collection,
# it points to the next free work in the reserve area.
#
# `.Alloc.limit`: points to the tip of the assignment stack,
# `.Alloc.ptr` cannot go past `.Alloc.limit`.
# Between `.Alloc.limit` and L3 sits the assignment stack
# which grows towards `.Alloc.ptr`.
#
# The following invariant is maintained for `.Alloc.ptr` and `assign_ptr` by
# the garbage collector's code at all times:
#
# `.Alloc.ptr` is always strictly less than `.Alloc.limit`.
# Hence there is always enough room for at least one assignment record
# at the tip of assignment stack.
#
# If the above invariant hadn't been maintained, we would've ended up
# in a situation where at the moment we're requested to record an
# assignment, `.Alloc.ptr` == `.Alloc.limit`. As there is no room to
# record the assignment a garbage collection has to run first.
# As the unrecorded assignment can point to a GC root, the garbage
# collection would've missed that root and removed a live object or
# multiple live objects...
#
# During a Major collection, the heap has the following form:
#
# Header
# |
# | Older generation objects
# | |
# | | Objects surviving last minor garbage collection
# | | |
# | | | Major garbage collection area
# | | | |
# v v v v
# +----+------------------+----------+------------------------------+
# |XXXX| Old Area | X | New Area |
# +----+------------------+----------+------------------------------+
# ^ ^ ^ ^ ^ ^ ^
# | | | | | |--> |
# | L0 L1 | L2 .Alloc.ptr .Alloc.limit, L4
# | |
# heap_start breakpoint
#
# `.Alloc.ptr` (allocation pointer): During a major collection, this points
# into the next free word in the new area.
#
# `.Alloc.limit`: During a major collection, this points to the tip of an
# empty assignment stack wich is the same as the limit of heap memory.
# `.Alloc.ptr` is not allowed to pass this value. If the live objects
# in the old area cannot fit in the new area, more memory is allocated
# and `.Alloc.limit` is adjusted accordingly.
#
# See the `.Alloc.ptr` < `.Alloc.limit` invariant descriptions above.
#
# breakpoint: Point where a major collection will occur. It is
# calculated by the following formula:
#
# breakpoint = MIN(L3-MAX(MAJOR0,MAJOR1)-MAX(MINOR0,MINOR1),
# L3-(L3-L0)/2)
#
# where (variables stored in the header):
# MAJOR0 = total size of objects in the new area after last major
# collection.
# MAJOR1 = (MAJOR0+MAJOR1)/2
# MINOR0 = total size of objects in the reserve area after last
# minor collection.
# MINOR1 = (MINOR0+MINOR1)/2
#
# The following assumptions are made in the garbage collection
# process:
#
# 1) Pointers on the Stack:
# Every word on the stack that ends in 0 (i.e., is even) and is
# a valid address in the heap is assumed to point to an object
# in the heap. Even heap addresses on the stack that are actually
# something else (e.g., raw integers) will probably cause an
# garbage collection error.
#
# 2) Object Layout:
# Besides the Int, String, and Bool objects (which are handled
# separately), the garbage collector assumes that each attribute
# in an object is a pointer to another object. It, however,
# still does as much as possible to verify this before actually
# updating any fields.
#
# 3) Pointer tests:
# In order to be verified as an object, a pointer must undergo
# certain tests:
#
# a) The pointer must point within the correct storage area.
# b) The word before the pointer (obj_eyecatch) must be the
# word 0xFFFF FFFF
# c) The word at the pointer must not be 0xFFFF FFFF (i.e.
# -1 cannot be a class tag)
#
# These tests are performed whenever any data could be a pointer
# to keep any non-pointers from being updated accidentally. The
# functions `.GenGC.check_copy` and `.GenGC.offset_copy` are responsible
# for these checks.
#
# 4) The size stored in the object does not include the word required
# to store the eyecatcher for the object in the heap. This allows
# the prototype objects to not require its own eyecatcher. Also,
# a size of 0 is invalid because it is used as a flag by the garbage
# collector to indicate a forwarding pointer in the `obj_disp` field.
#
# 5) Roots are contained in the following areas: the stack, registers
# specified in the REG mask, and the assignment table.
########################################
#
# GenGC header offsets from `.Platform.heap_start`
#
.GenGC.HDR_SIZE = 80 # size of GenGC header
.GenGC.HDR_L0 = 0 # old area start
.GenGC.HDR_L1 = 8 # old area end/reserve area start
.GenGC.HDR_L2 = 16 # reserve area end/work area start
.GenGC.HDR_L3 = 24 # assignment table end/unused start
.GenGC.HDR_L4 = 32 # unused end
.GenGC.HDR_MAJOR0 = 40 # total size of objects in the new area
# after last major collection
.GenGC.HDR_MAJOR1 = 48 # (MAJOR0 + MAJOR1) / 2
.GenGC.HDR_MINOR0 = 56 # size of all live objects collected
# during last minor collection
.GenGC.HDR_MINOR1 = 64 # (MINOR0 + MINOR1) / 2
.GenGC.HDR_STACK_BASE = 72 # base of stack
#
# Granularity of heap expansion
#
# The heap is always expanded in multiples of 2^k, where
# k is the granularity.
#
.GenGC.HEAP_PAGE = 32768 # in bytes
#
# Old to usable heap size ratio
#
# After a major collection, the ratio of size of old area to the usable
# size of the heap is at most 1/(2^k) where k is the value provided.
#
.GenGC.OLD_RATIO = 2 # 1/(2^2)=.25=25%
#
# Initialization
#
# Sets up the header information block for the garbage collector.
# This block is located at the start of the heap (`Platform.heap_start`)
# and includes information needed by the garbage collector. It
# also calculates the boundary for the reserve and work areas and
# sets the L2 pointer accordingly, rounding off in favor of the
# reserve area.
#
# INPUT:
# %rdi: the base of stack to stop checking for GC roots at.
# (remember the stack grows down,
# so the base is at the highest address)
#
# OUTPUT:
# none
#
# Registers modified:
# %rax, %rdi, %rsi, .Platform.alloc
#
.global .GenGC.init
.GenGC.init:
STACK_BASE_SIZE = 8
STACK_BASE = -STACK_BASE_SIZE
PAD_SIZE = 8
FRAME_SIZE = STACK_BASE_SIZE + PAD_SIZE
pushq %rbp
movq %rsp, %rbp
subq $FRAME_SIZE, %rsp
movq %rdi, STACK_BASE(%rbp)
movq $.GenGC.HEAP_PAGE, %rdi # allocate initial heap space
call .Platform.alloc
movq .Platform.heap_start(%rip), %rdi
movq $.GenGC.HDR_SIZE, %rax
addq %rdi, %rax # %rax contains the first addr past the header
movq %rax, .GenGC.HDR_L0(%rdi) # init the header's L0 field
movq %rax, .GenGC.HDR_L1(%rdi) # init the header's L1 field
movq .Platform.heap_end(%rip), %rsi
subq %rax, %rsi # heap_end - (heap_start + .GenGC.HDR_SIZE)
sarq $1, %rsi # (heap_end - (heap_start + .GenGC.HDR_SIZE)) / 2
andq $(-8), %rsi # round down to the closest smaller multiple of 8 bytes
# since our object sizes are multiples of 8 bytes
jz .GenGC.init.abort # heap initially too small
movq .Platform.heap_end(%rip), %rax
movq %rax, .GenGC.HDR_L3(%rdi) # initially the end of work area is at the heap end
movq %rax, .Alloc.limit(%rip) # initially the tip of assign stack is at the end of work area
movq %rax, .GenGC.HDR_L4(%rdi) # initially the end of unused area is at the heap end
subq %rsi, %rax # %rsi contains the work area size
# L3 - %rsi = reserve area end/work area start
movq %rax, .GenGC.HDR_L2(%rdi) # store the calculated start of work area
movq %rax, .Alloc.ptr(%rip) # initially the allocation pointer is at the start of work area
movq $0, .GenGC.HDR_MAJOR0(%rdi) # init histories with zeros
movq $0, .GenGC.HDR_MAJOR1(%rdi)
movq $0, .GenGC.HDR_MINOR0(%rdi)
movq $0, .GenGC.HDR_MINOR1(%rdi)
movq STACK_BASE(%rbp), %rax
movq %rax, .GenGC.HDR_STACK_BASE(%rdi) # init stack base
movq .MemoryManager.IS_TESTING(%rip), %rax # check if heap testing enabled
testq %rax, %rax
jz .GenGC.init.heap_test_disabled
movq $.GenGC.MSG_INITED_IN_TEST_ASCII, %rdi
movq $.GenGC.MSG_INITED_IN_TEST_LEN, %rsi
call .Runtime.print_ln
jmp .GenGC.init.ok
.GenGC.init.heap_test_disabled:
# movq $.GenGC.MSG_INIT_OK_ASCII, %rdi
# movq $.GenGC.MSG_INIT_OK_LEN, %rsi
# call .Runtime.print_ln
.GenGC.init.ok:
# call .GenGC.print_state
movq %rbp, %rsp
popq %rbp
ret
.GenGC.init.abort:
movq $.GenGC.MSG_INIT_ERROR_ASCII, %rdi
movq $.GenGC.MSG_INIT_ERROR_LEN, %rsi
call .Runtime.print_ln
movq $1, %rdi
jmp .Platform.exit_process
#
# Record an Assignment in the Assignment Stack
#
# The GC's code guarantees `.Alloc.ptr` is always strictly less than `.Alloc.limit`.
# Hence there is always enough room for at least one assignment record
# at the tip of assignment stack.
#
# If the above invariant hadn't been maintained, we would've ended up
# in a situation where at the moment we're requested to record an
# assignment, `.Alloc.ptr` == `.Alloc.limit`. As there is no room to
# record the assignment a garbage collection has to run first.
# As the unrecorded assignment can point to a GC root, the garbage
# collection would've missed that root and removed a live object or
# multiple live objects...
#
# INPUT:
# %rdi: pointer to an obj's attribute (a pointer itself) being assigned to
#
# OUTPUT:
# None
#
# Registers modified:
# %rax, %rdi, %rsi, .GenGC.collect
#
.global .GenGC.on_assign
.GenGC.on_assign:
POINTER_SIZE = 8
pushq %rbp
movq %rsp, %rbp
# TODO: Preserve %rdi?
movq .Platform.heap_start(%rip), %rsi
# An assignment is a GC root only if
# 1) The obj's field that is being assigned to is within [L0, L1) (Old Area)
# 2) The assigned value points to an object withing [L2, .Alloc.limit) (Work Area).
# Otherwise, it points to an obj withing an area not managed by
# the minor collection:
# - Either [L0, L1) (Old Area)
# - Or outside of the heap -- e.g., a predefined object in the data segment
#
# Check if the pointer is within [L0, L1) (Old Area)
cmpq .GenGC.HDR_L0(%rsi), %rdi
jl .GenGC.on_assign.done # if (%rdi < L0)
# go to .GenGC.on_assign.done
cmpq .GenGC.HDR_L1(%rsi), %rdi
jge .GenGC.on_assign.done # if (%rdi >= L1)
# go to .GenGC.on_assign.done
# Check if the obj pointed to is within [L2, .Alloc.limit) (Work Area)
movq 0(%rdi), %rax # %rax = the obj being pointed to
cmpq .GenGC.HDR_L2(%rsi), %rax
jl .GenGC.on_assign.done # if (%rax < L2)
# go to .GenGC.on_assign.done
cmpq .Alloc.limit(%rip), %rax
jge .GenGC.on_assign.done # if (%rax >= .Alloc.limit)
# go to .GenGC.on_assign.done
# OK, we do want to track this assignment
movq .Alloc.limit(%rip), %rax
subq $POINTER_SIZE, %rax
movq %rax, .Alloc.limit(%rip) # make room in the assignment stack
movq %rdi, 0(%rax) # place pointer to the pointer being assigned to
# at the tip of assignment stack
cmpq .Alloc.ptr(%rip), %rax # if `.Alloc.ptr` and `.Alloc.limit` have met
# we'll have to collect garbage
jg .GenGC.on_assign.done # if (.Alloc.limit > .Alloc.ptr) go to ...
xor %edi, %edi # we request to allocate 0 bytes
# as we only need to collect garbage
movq %rbp, %rsi # the tip of stack to start checking for roots from
call .GenGC.collect
.GenGC.on_assign.done:
movq %rbp, %rsp
popq %rbp
ret
#
# Generational Garbage Collection
#
# This function implements the generational garbage collection.
#
# It first calls the minor collector, `.GenGC.minor_collect`, and then
# updates its history in the header.
#
# The breakpoint is then calculated. If the breakpoint is reached or
# there is still not enough room to allocate the requested size,
# a major garbage collection takes place by calling `.GenGC.major_collect`.
#
# After the major collection, the size of the old area is analyzed.
# If it is greater than 1/(2^GenGC_OLDRATIO) of the total usable heap
# size (L0 to L3), the heap is expanded.
#
# If there is still not enough room to allocate the requested size,
# the heap is expanded further to make sure that the specified
# amount of memory can be allocated.
#
# If there is enough room in the unused area (L3 to L4),
# this memory is used and the heap is not expanded.
#
# The `.Alloc.limit` and `.Alloc.ptr` pointers are then set
# as well as the L2 pointer.
#
# If a major collection is not done, the X area is incorporated
# into the old area (i.e. the value of L2 is moved into L1) and
# `.Alloc.limit`, `.Alloc.ptr`, and L2 are then set.
#
# INPUT:
# %rdi: requested allocation size in bytes
# %rsi: the tip of stack to start checking for roots from
# %rdx: force a major collection if %rdx != 0
#
# OUTPUT:
# %rdi: requested allocation size in bytes (unchanged)
#
# GLOBALS MODIFIED:
# L1, L2, L3, L4, .Alloc.ptr, .Alloc.limit,
# MINOR0, MINOR1, MAJOR0, MAJOR1
#
# Registers modified:
# %rax, %rdi, %rsi, %rcx, %rdx, .GenGC.minor_collect, .GenGC.major_collect
#
.global .GenGC.collect
.GenGC.collect:
ALLOC_SIZE_SIZE = 8
ALLOC_SIZE = -ALLOC_SIZE_SIZE
STACK_TIP_SIZE = 8
STACK_TIP = -(ALLOC_SIZE_SIZE + STACK_TIP_SIZE)
FORCE_MAJOR_SIZE = 8
FORCE_MAJOR = -(ALLOC_SIZE_SIZE + STACK_TIP_SIZE + FORCE_MAJOR_SIZE)
PAD_SIZE = 8
FRAME_SIZE = ALLOC_SIZE_SIZE + STACK_TIP_SIZE + FORCE_MAJOR_SIZE + PAD_SIZE
pushq %rbp
movq %rsp, %rbp
subq $FRAME_SIZE, %rsp
movq %rdi, ALLOC_SIZE(%rbp)
movq %rsi, STACK_TIP(%rbp)
movq %rdx, FORCE_MAJOR(%rbp)
# movq $.GenGC.MSG_COLLECTING_ASCII, %rdi
# movq $.GenGC.MSG_COLLECTING_LEN, %rsi
# call .Runtime.print_ln
# movq $.GenGC.MSG_MINOR_ASCII, %rdi
# movq $.GenGC.MSG_MINOR_LEN, %rsi
# call .Runtime.print_ln
# call .Runtime.out_nl
movq STACK_TIP(%rbp), %rdi
call .GenGC.minor_collect # %rax contains the size of all collected live objects
# call .GenGC.print_state
# %rdi = heap_start
movq .Platform.heap_start(%rip), %rdi
# Update MINOR0, MINOR1
movq %rax, .GenGC.HDR_MINOR0(%rdi) # MINOR0 = the size of all collected live objects
movq .GenGC.HDR_MINOR1(%rdi), %rsi # %rsi = MINOR1
addq %rax, %rsi # %rsi = MINOR1 + MINOR0
sarq $1, %rsi # %rsi = (MINOR0 + MINOR1) / 2
movq %rsi, .GenGC.HDR_MINOR1(%rdi) # MINOR1 = (MINOR0 + MINOR1) / 2
# See if we were requested to perform a major collection unconditionally.
movq FORCE_MAJOR(%rbp), %rdx
testq %rdx, %rdx
jnz .GenGC.collect.do_major
# breakpoint = MIN(L3 - MAX(MAJOR0, MAJOR1) - MAX(MINOR0, MINOR1),
# L3 - (L3 - L0) / 2)
#
# L3 - MAX(MAJOR0, MAJOR1) - MAX(MINOR0, MINOR1)
# %rdx = MAX(MINOR0, MINOR1)
movq %rax, %rdx # %rdx = MINOR0
cmpq %rsi, %rax # MINOR0 >= MINOR1?
jge .GenGC.collect.calc_max_of_majors # if yes, go to .GenGC.collect.calc_max_of_majors
movq %rsi, %rdx # if no, %rdx = MINOR1
.GenGC.collect.calc_max_of_majors:
# %rcx = MAX(MAJOR0, MAJOR1)
movq .GenGC.HDR_MAJOR0(%rdi), %rcx # %rcx = MAJOR0
movq .GenGC.HDR_MAJOR1(%rdi), %rax # %rax = MAJOR1
cmpq %rax, %rcx # MAJOR0 >= MAJOR1?
jge .GenGC.collect.calc_min_breakpoint # if yes, go to .GenGC.collect.calc_min_breakpoint
movq %rax, %rcx # if no, %rcx = MAJOR1
.GenGC.collect.calc_min_breakpoint:
movq .GenGC.HDR_L3(%rdi), %rsi # %rsi = L3
subq %rcx, %rsi # %rsi = L3 - MAX(MAJOR0, MAJOR1)
subq %rdx, %rsi # %rsi = L3 - MAX(MAJOR0, MAJOR1) - MAX(MINOR0, MINOR1)
# L3 - (L3 - L0) / 2
movq .GenGC.HDR_L3(%rdi), %rax # %rax = L3
movq %rax, %rcx # %rcx = L3
subq .GenGC.HDR_L0(%rdi), %rax # %rax = L3 - L0
sarq $1, %rax # %rax = (L3 - L0) / 2
subq %rax, %rcx # %rcx = L3 - (L3 - L0) / 2
# %rcx = MIN(%rcx, %rsi) = breakpoint
cmpq %rsi, %rcx # %rcx <= %rsi?
jle .GenGC.collect.check_breakpoint # if yes, go to .GenGC.collect.check_breakpoint
movq %rsi, %rcx # if no, %rcx = %rsi
.GenGC.collect.check_breakpoint:
# %rcx contains the breakpoint value
movq .GenGC.HDR_L1(%rdi), %rax
cmpq %rcx, %rax # Has L1 (the end of Old Area) crossed the breakpoint?
# (%rax >= %rcx?)
jge .GenGC.collect.do_major # if yes, perform a major collection
# If no, update L1, set up the new Reserve/Work areas,
# reset `.Alloc.ptr`, and `.Alloc.limit`, etc
movq .GenGC.HDR_L2(%rdi), %rcx # %rcx = L2
movq .GenGC.HDR_L3(%rdi), %rdx # %rdx = L3
movq %rdx, %rsi # %rsi = L3
# Calculate Reserve/Work areas boundary
subq %rcx, %rdx # %rdx = L3 - L2
sarq $1, %rdx # %rdx = (L3 - L2) / 2
andq $(-8), %rdx # %rdx = the nearest smaller multiple of 8
# so Reserve Area >= Work Area by 8 bytes
subq %rdx, %rsi # %rsi = the new L2
# (L3 - round_down((L3 - L2) / 2))
movq %rsi, %rcx # %rcx = the new L2
# Enough space to allocate the requested size?
movq ALLOC_SIZE(%rbp), %rax # %rax = the requested allocation size
addq %rax, %rsi # %rsi = (the new L2) + alloc size
movq .GenGC.HDR_L3(%rdi), %rdx # %rdx = L3
cmpq %rdx, %rsi # %rsi >= L3?
jge .GenGC.collect.do_major # if yes, there's not engough space,
# we'll have to preform a major collection
# No major collection required
movq .GenGC.HDR_L2(%rdi), %rax
movq %rax, .GenGC.HDR_L1(%rdi) # include the live object collected
# by `.GenGC.minor_collect` into Old Area
# %rcx contains the new L2 value
movq %rcx, .GenGC.HDR_L2(%rdi) # set up the new Reserver/Work boundary
movq %rcx, .Alloc.ptr(%rip) # set `.Alloc.ptr` at the start of Work Area
movq %rdx, .Alloc.limit(%rip) # set `.Alloc.limit` to L3 (Work Area's end)
# effectively clearing the assignment stack.
# Garbage collecting the young gen results in
# no old gen objects pointing to young gen
# objects anymore, so we can clear the stack.
jmp .GenGC.collect.done
.GenGC.collect.do_major:
# movq $.GenGC.MSG_MAJOR_ASCII, %rdi
# movq $.GenGC.MSG_MAJOR_LEN, %rsi
# call .Runtime.print_ln
# call .Runtime.out_nl
movq STACK_TIP(%rbp), %rdi
call .GenGC.major_collect # %rax: the size of all collected live objects
# L1: the new end of Old Area
# call .GenGC.print_state
# %rdi = heap_start
movq .Platform.heap_start(%rip), %rdi
# Update MAJOR0, MAJOR1
movq %rax, .GenGC.HDR_MAJOR0(%rdi) # MAJOR0 = total size of objects in the new area
# after the major collection.
movq .GenGC.HDR_MAJOR1(%rdi), %rsi # %rsi = MAJOR1
addq %rax, %rsi # %rsi = MAJOR1 + MAJOR0
sarq $1, %rsi # %rsi = (MAJOR0 + MAJOR1) / 2
movq %rsi, .GenGC.HDR_MAJOR1(%rdi) # MAJOR1 = (MAJOR0 + MAJOR1) / 2
# Calculate how much we need to expand the heap (if at all),
# to preserve the chosen Old Area/Heap size ratio.
# Calculate the max end of Old Area that still stays
# within the chosen ratio to the total heap size.
# Place the value in %rdx
movq .GenGC.HDR_L0(%rdi), %rdx # %rdx = L0
movq .GenGC.HDR_L3(%rdi), %rcx # %rcx = L3
movq %rcx, %rax # %rax = L3
subq %rdx, %rax # %rax = L3 - L0
sarq $.GenGC.OLD_RATIO, %rax # %rax = (L3 - L0) / 2^.GenGC.OLD_RATIO
addq %rax, %rdx # %rdx = L0 + (L3 - L0) / 2^.GenGC.OLD_RATIO
# = L0 + sizeof(max Old Area)
# Calculate
# %rcx = the new end of Old Area - the max end of Old Area.
# (Remember `.GenGC.major_collect` places the new end of Old Area into L1))
#
# If %rcx <= 0
# (i.e. the new size of Old Area is less than the max size of Old Area)
# we don't need to allocate addtional memory to restore the size ratio.
# Although we don't branch physically, further calculations on %rcx can
# be logically ignored.
# We'll check the memory to allocate's size is not <= 0 later on.
#
# If %rcx > 0
# (i.e. the new size of Old Area is greater than the max size of Old Area)
# we need to allocate %rcx * 2^.GenGC.OLD_RATIO memory
# to restore the Old Area/Heap size ratio.
#
# Keep in mind,
# L1 - (L0 + (L3 - L0) / 2^.GenGC.OLD_RATIO) =
# (L0 + sizeof(the new Old Area)) - (L0 + sizeof(max Old Area)) =
# sizeof(the new Old Area) - sizeof(max Old Area)
movq .GenGC.HDR_L1(%rdi), %rcx # %rcx = L1
# (`.GenGC.major_collect` places
# the new end of Old Area into L1)
subq %rdx, %rcx # %rcx = L1 - (L0 + (L3 - L0) / 2^.GenGC.OLD_RATIO)
salq $.GenGC.OLD_RATIO, %rcx # %rcx = %rcx * 2^.GenGC.OLD_RATIO
# Calculate how much we need to expand the heap (if at all),
# to accomodate the requested allocation size in the Reserve/Work areas
# Calculate the new Reserve/Work areas boundary's position.
# Which is the same as Work Area's start position.
# Place the value into %rdx
movq .GenGC.HDR_L3(%rdi), %rsi # %rsi = L3
subq .GenGC.HDR_L0(%rdi), %rsi # %rsi = L3 - L0
sarq $1, %rsi # %rsi = (L3 - L0) / 2
andq $(-8), %rsi # %rsi = ((L3 - L1) / 2) & (-8)
movq .GenGC.HDR_L3(%rdi), %rdx # %rdx = L3
subq %rsi, %rdx # %rdx = L3 - ((L3 - L1) / 2) & (-8)
# = Reserve/Work areas boundary
# = Work Area start
# Now, see whether the requested allocation size fits withing Work Area's boundaries.
# Calculate the difference between (Work Area start + requested alloc size)
# and Work Area's end position (L3).
# Place the value into %rdx.
#
# If the difference <= 0, although we don't branch physically,
# further calculations on %rdx can be logically ignored.
# We'll check the memory to allocate's size is not <= 0 later on.
#
# If the difference > 0 we need to allocate %rdx * 2 memory
# as we guarantee Reserve Area to be >= the size of Work Area we need
# %rdx bytes for Work Area + %rdx bytes for Reserve Area.
addq ALLOC_SIZE(%rbp), %rdx # %rdx = Work Area start + requested alloc size
subq .GenGC.HDR_L3(%rdi), %rdx # %rdx = (Work Area's start + requested alloc size) - L3
addq $8, %rdx # %rdx += 8 -- adjust for round off errors
# (interger division by 2, etc)
salq $1, %rdx # %rdx *= 2 -- need to allocate this much memory
# %rcx = max(%rcx, %rdx)
cmpq %rdx, %rcx
jge .GenGC.collect.ensure_heap_size # if (%rcx >= %rdx) go to .GenGC.collect.ensure_heap_size
movq %rdx, %rcx # else %rcx = %rdx
.GenGC.collect.ensure_heap_size:
# If max(%rcx, %rdx) <= 0, that means:
# 1) both %rcx and %rdx are <= 0
# 2) Old Area/Heap size ratio is preserved
# 3) We have enough space in Work Area to
# accomodate the requested allocation size
cmpq $0, %rcx
jle .GenGC.collect.set_Alloc_limit_and_L2 # if (%rcx <= 0)
# go to .GenGC.collect.set_Alloc_limit_and_L2
# %rcx: we need to expand the heap by at least this number of bytes.
# Round up %rcx to the nearest greater multiple of .GenGC.HEAP_PAGE (e.g., 32768 bytes):
# %rcx = (%rcx + 32767) & (-32768)
movq $.GenGC.HEAP_PAGE, %rax # %rax = 32768 = 00000000_00000000_10000000_00000000
decq %rax # %rax = 32767 = 00000000_00000000_01111111_11111111
addq %rax, %rcx # %rcx = %rcx + 32767
notq %rax # %rax = -32768 = 11111111_11111111_10000000_00000000
andq %rax, %rcx # %rcx = %rcx & (-32768)
# %rcx: the total number of bytes to expand the heap by.
# (a multiple of .GenGC.HEAP_PAGE).
# See how much of the total expansion is covered by Unused.
movq .GenGC.HDR_L4(%rdi), %rax # %rax = L4
subq .GenGC.HDR_L3(%rdi), %rax # %rax = L4 - L3 = sizeof(Unused)
movq %rcx, %rdx # %rdx = %rcx
# = total expansion size
subq %rax, %rdx # %rdx = total expansion size - sizeof(Unused)
jg .GenGC.collect.platform_allocate # if ((total expansion size) > sizeof(Unused))
# go to .GenGC.collect.platform_allocate
# We have enough Unused space to cover the required expansion
# without allocating any additional memory from the OS.
movq .GenGC.HDR_L3(%rdi), %rax # %rax = L3
addq %rcx, %rax # %rax = L3 + total expansion size
movq %rax, .GenGC.HDR_L3(%rdi) # L3 = L3 + total expansion size
movq %rax, .Alloc.limit(%rip) # .Alloc.limit = L3 + total expansion size
# (therefore, the assign stack size = 0)
jmp .GenGC.collect.set_L2
.GenGC.collect.platform_allocate:
# %rdx = total expansion size - sizeof(Unused)
movq %rdx, %rdi # %rdi = requested alloc size
call .Platform.alloc # %rax = allocated memory block's start
movq .Platform.heap_start(%rip), %rdi # %rdi = heap_start
movq .Platform.heap_end(%rip), %rax # %rax = heap_end after the allocation
movq %rax, .GenGC.HDR_L3(%rdi) # L3 = heap_end after the allocation
movq %rax, .Alloc.limit(%rip) # .Alloc.limit = heap_end after the allocation
# (therefore, the assign stack size = 0)
movq %rax, .GenGC.HDR_L4(%rdi) # L4 = heap_end after the allocation
# (therefore, sizeof(Unused) = 0 at this point)
jmp .GenGC.collect.set_L2
.GenGC.collect.set_Alloc_limit_and_L2:
movq .GenGC.HDR_L3(%rdi), %rax # %rax = L3
movq %rax, .Alloc.limit(%rip) # .Alloc.limit = L3
# (therefore, the assign stack size = 0)
.GenGC.collect.set_L2:
# %rax must be equal to L3 at this point
movq %rax, %rsi # %rsi = %rax = L3
subq .GenGC.HDR_L1(%rdi), %rax # %rax = L3 - L1
sarq %rax # %rax = (L3 - L1) / 2
andq $(-8), %rax # %rax = ((L3 - L1) / 2) & (-8)
# = sizeof(Work Area)
subq %rax, %rsi # %rsi = L3 - sizeof(Work Area)
# = Reserve/Work areas boundary
movq %rsi, .GenGC.HDR_L2(%rdi) # L2 = %rsi
movq %rsi, .Alloc.ptr(%rip) # .Alloc.ptr = %rsi
.GenGC.collect.done:
# call .GenGC.print_state
# Zero out the new generation (the new Reserve and Work Area)
# to help catch missing pointers
movq .Platform.heap_start(%rip), %rax
movq .GenGC.HDR_L1(%rax), %rax
.GenGC.collect.work_area_clear_loop:
movq $0, 0(%rax) # zero out the quad at %rax
addq $8, %rax
cmpq .Alloc.limit(%rip), %rax # %rax < `.Alloc.limit`
jl .GenGC.collect.work_area_clear_loop # if yes, we haven't reached
# the end of Work Area yet
movq ALLOC_SIZE(%rbp), %rdi # restore requested allocation size in bytes
movq %rbp, %rsp
popq %rbp
ret
#
# Check and Copy an Object
#
# Checks that the input pointer points to a heap object.
#
# If so, it then checks the object for a forwarding pointer by
# checking the object's size for 0.
#
# If found, the forwarding pointer is returned.
# Else, the object is copied to `.Alloc.ptr` and a pointer to
# this copy is returned.
#
# The following tests are done to determine if the object is
# a heap object:
#
# 1) The pointer is a multiple of 8 (a quad is our chosen minimal granularity)
# 2) The pointer is within the specified limits
# 3) The word before the pointer is the eye catcher 0xFFFF_FFFF
# 4) The word at the pointer is a valid tag (i.e. not equal to
# 0xFFFF_FFFF)
#
# INPUT:
# %rdi: pointer to check and copy
# %rsi: lower bound object should be within
# %rdx: upper bound object should be within
#
# OUTPUT:
# %rax: if %rdi points to a heap object
# then it is set to the location of copied object.
# Else, unchanged value from %rdi.
# %rsi: lower bound object should be within (unchanged)
# %rdx: upper bound object should be within (unchanged)
#
# GLOBALS MODIFIED:
# .Alloc.ptr
#
# Registers modified:
# %rax, %rdi, %rcx
#
.global .GenGC.check_copy
.GenGC.check_copy:
POINTER_SIZE = 8
POINTER = -POINTER_SIZE
LOWER_BOUND_SIZE = 8
LOWER_BOUND = -(POINTER_SIZE + LOWER_BOUND_SIZE)
UPPER_BOUND_SIZE = 8
UPPER_BOUND = -(POINTER_SIZE + LOWER_BOUND_SIZE + UPPER_BOUND_SIZE)
PAD_SIZE = 8
FRAME_SIZE = (POINTER_SIZE + LOWER_BOUND_SIZE + UPPER_BOUND_SIZE + PAD_SIZE)
pushq %rbp
movq %rsp, %rbp
subq $FRAME_SIZE, %rsp
movq %rdi, POINTER(%rbp)
movq %rsi, LOWER_BOUND(%rbp)
movq %rdx, UPPER_BOUND(%rbp)
movq %rdi, %rax # if a check doesn't pass
# we promised %rax = %rdi
# If the pointer is a muliple of 8,
# its least significant 3 bits are 000
testq $7, %rdi
jnz .GenGC.check_copy.done # if (%rdi % 8 != 0)
# go to .GenGC.check_copy.done
# Check if the pointer is within [%rsi, %rdx)
cmpq %rsi, %rdi
jl .GenGC.check_copy.done # if (%rdi < %rsi)
# go to .GenGC.check_copy.done
cmpq %rdx, %rdi
jge .GenGC.check_copy.done # if (%rdi >= %rdx)
# go to .GenGC.check_copy.done
# Check the eye catcher is present
cmpq $EYE_CATCH, OBJ_EYE_CATCH(%rdi)
je .GenGC.check_copy.check_tag
call .GC.abort # if no eye catcher,
# go to .GC.abort
.GenGC.check_copy.check_tag:
# Check the object's tag != EYE_CATCH
cmpq $EYE_CATCH, OBJ_TAG(%rdi)
je .GenGC.check_copy.done # if (tag == $EYE_CATCH)
# go to .GenGC.check_copy.done
movq OBJ_SIZE(%rdi), %rsi # %rsi = sizeof(obj) in quads
testq %rsi, %rsi
jz .GenGC.check_copy.copy_done # if (sizeof(obj) == 0)
# the source obj has already been copied
.GenGC.check_copy.copy:
# The checks have passed,
# we're going to copy the object now.
movq .Alloc.ptr(%rip), %rcx
addq $8, %rcx # %rcx = the start of copy obj
# (skipped the eye catcher)
movq %rcx, %rdx # %rdx = the start of copy obj
movq $EYE_CATCH, OBJ_EYE_CATCH(%rdx) # place the eye catcher before the copy obj
salq $3, %rsi # %rsi = sizeof(obj) in quads * 8 = sizeof(obj) in bytes
addq %rdi, %rsi # %rsi = the start of source obj + sizeof(obj) in bytes
# = the end of source obj
# %rdi: the start of source object
# %rsi: the end of source obj
# %rcx: the start of destination (copy) object
# %rdx: the start of destination (copy) object
.GenGC.check_copy.copy_loop:
movq 0(%rdi), %rax
movq %rax, 0(%rdx)
addq $8, %rdi
addq $8, %rdx
cmpq %rsi, %rdi
jl .GenGC.check_copy.copy_loop # if (%rdi < %rsi)
# go to .GenGC.check_copy.copy_loop
# %rcx: the start of destination (copy) object
# %rdx: the end of destination (copy) object
movq %rdx, .Alloc.ptr(%rip) # .Alloc.ptr = the end of dest (copy) obj
# Mark the source object as copied
movq POINTER(%rbp), %rdi # %rdi = the start of source obj
movq $0, OBJ_SIZE(%rdi) # put 0 into the source obj's size
movq %rcx, OBJ_VTAB(%rdi) # put a forwarding pointer to the copy
# into the source obj's vtab slot
.GenGC.check_copy.copy_done:
# %rdi must be = the start of source obj
movq OBJ_VTAB(%rdi), %rax # %rax = a pointer to the obj copy
movq LOWER_BOUND(%rbp), %rsi # %rsi = the original value
movq UPPER_BOUND(%rbp), %rdx # %rdx = the original value
.GenGC.check_copy.done:
movq %rbp, %rsp
popq %rbp
ret