-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap.s
1639 lines (1479 loc) · 43.6 KB
/
trap.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
.kdata
__m1_: .asciiz " Exception "
__m2_: .asciiz " Execution aborted\n"
__e0_: .asciiz " [Interrupt] "
__e1_: .asciiz ""
__e2_: .asciiz ""
__e3_: .asciiz ""
__e4_: .asciiz " [Unaligned address in inst/data fetch] "
__e5_: .asciiz " [Unaligned address in store] "
__e6_: .asciiz " [Bad address in text read] "
__e7_: .asciiz " [Bad address in data/stack read] "
__e8_: .asciiz " [Error in syscall] "
__e9_: .asciiz " [Breakpoint/Division by 0] "
__e10_: .asciiz " [Reserved instruction] "
__e11_: .asciiz ""
__e12_: .asciiz " [Arithmetic overflow] "
__e13_: .asciiz " [Inexact floating point result] "
__e14_: .asciiz " [Invalid floating point result] "
__e15_: .asciiz " [Divide by 0] "
__e16_: .asciiz " [Floating point overflow] "
__e17_: .asciiz " [Floating point underflow] "
__excp: .word __e0_,__e1_,__e2_,__e3_,__e4_,__e5_,__e6_,__e7_,__e8_,__e9_
.word __e10_,__e11_,__e12_,__e13_,__e14_,__e15_,__e16_,__e17_
s1: .word 0
s2: .word 0
.ktext 0x80000080
.set noat
move $at $k1
.set at
sw $v0 s1
sw $a0 s2
mfc0 $k0 $13
sgt $v0 $k0 0x44 # ignore interrupt exceptions
bgtz $v0 ret
addu $0 $0 0
li $v0 4 # syscall 4 (print_str)
la $a0 __m1_
syscall
li $v0 1 # syscall 1 (print_int)
srl $a0 $k0 2
syscall
li $v0 4 # syscall 4 (print_str)
lw $a0 __excp($k0)
syscall
li $v0 4
la $a0 __m2_
syscall
li $v0 10 # Exit upon all exceptions
syscall # syscall 10 (exit)
ret: lw $v0 s1
lw $a0 s2
mfc0 $k0 $14
.set noat
move $k1 $at
.set at
rfe # Return from exception handler
addiu $k0 $k0 4 # Return to next instruction
jr $k0
#Up for SPIM
#DOWN for COOL RUNTIME
# Standard startup code. Invoke the routine main with no arguments.
.data
_abort_msg: .asciiz "Abort called from class "
_colon_msg: .asciiz ":"
_dispatch_msg: .asciiz ": Dispatch to void.\n"
_cabort_msg: .asciiz "No match in case statement for Class "
_cabort_msg2: .asciiz "Match on void in case statement.\n"
_nl: .asciiz "\n"
_term_msg: .asciiz "COOL program successfully executed\n"
_sabort_msg1: .asciiz "Index to substr is negative\n"
_sabort_msg2: .asciiz "Index to substr is too big\n"
_sabort_msg3: .asciiz "Length to substr too long\n"
_sabort_msg4: .asciiz "Length to substr is negative\n"
_sabort_msg: .asciiz "Execution aborted.\n"
_objcopy_msg: .asciiz "Object.copy: Invalid object size.\n"
_gc_abort_msg: .asciiz "GC bug!\n"
#
# Messages for the GenGC garabge collector
#
_GenGC_INITERROR: .asciiz "GenGC: Unable to initialize the garbage collector.\n"
_GenGC_COLLECT: .asciiz "Garbage collecting ...\n"
_GenGC_Major: .asciiz "Major ...\n"
_GenGC_Minor: .asciiz "Minor ...\n"
#_GenGC_COLLECT: .asciiz ""
_GenGC_MINORERROR: .asciiz "GenGC: Error during minor garbage collection.\n"
_GenGC_MAJORERROR: .asciiz "GenGC: Error during major garbage collection.\n"
#
# Messages for the NoGC garabge collector
#
_NoGC_COLLECT: .asciiz "Increasing heap...\n"
#_NoGC_COLLECT: .asciiz ""
.align 2
#
# Define some constants
#
obj_eyecatch=-4 # Unique id to verify any object
obj_tag=0
obj_size=4
obj_disp=8
obj_attr=12
int_slot=12
bool_slot=12
str_size=12 # This is a pointer to an Int object!!!
str_field=16 # The beginning of the ascii sequence
str_maxsize=1026 # the maximum string length
MemMgr_REG_MASK=0x007F0000
.text
.globl __start
__start:
li $v0 9
move $a0 $zero
syscall # sbrk
move $a0 $sp # initialize the garbage collector
li $a1 MemMgr_REG_MASK
move $a2 $v0
jal _MemMgr_Init
la $a0 Main_protObj # create the Main object
jal Object.copy # Call copy
addiu $sp $sp -4
sw $a0 4($sp) # save the Main object on the stack
move $s0 $a0
jal Main_init # initialize the Main object
jal Main.main # Invoke main method
addiu $sp $sp 4
la $a0 _term_msg # show terminal message
li $v0 4
syscall
li $v0 10
syscall # syscall 10 (exit)
.globl equality_test
equality_test: # ops in $t1 $t2
# true in A0, false in A1
beq $t1 $zero _eq_false # $t2 can't also be void
beq $t2 $zero _eq_false # $t1 can't also be void
lw $v0 obj_tag($t1) # get tags
lw $v1 obj_tag($t2)
bne $v1 $v0 _eq_false # compare tags
lw $a2 _int_tag # load int tag
beq $v1 $a2 _eq_int # Integers
lw $a2 _bool_tag # load bool tag
beq $v1 $a2 _eq_int # Booleans
lw $a2 _string_tag # load string tag
bne $v1 $a2 _eq_false # Not a primitive type
_eq_str: # handle strings
lw $v0, str_size($t1) # get string size objs
lw $v1, str_size($t2)
lw $v0, int_slot($v0) # get string sizes
lw $v1, int_slot($v1)
bne $v1 $v0 _eq_false
beqz $v1 _eq_true # 0 length strings are equal
add $t1 str_field
add $t2 str_field
move $t0 $v0 # Keep string length as counter
_eq_l1:
lbu $v0,0($t1) # get char
add $t1 1
lbu $v1,0($t2)
add $t2 1
bne $v1 $v0 _eq_false
addiu $t0 $t0 -1
bnez $t0 _eq_l1
b _eq_true # end of strings
_eq_int: # handles booleans and ints
lw $v0,int_slot($t1) # load values
lw $v1,int_slot($t2)
bne $v1 $v0 _eq_false
_eq_true:
jr $ra # return true
_eq_false:
move $a0 $a1 # move false into accumulator
jr $ra
#
# _dispatch_abort
#
# filename in $a0
# line number in $t1
#
# Prints error message and exits.
# Called on dispatch to void.
#
.globl _dispatch_abort
_dispatch_abort:
sw $t1 0($sp) # save line number
addiu $sp $sp -4
addiu $a0 $a0 str_field # adjust to beginning of string
li $v0 4
syscall # print file name
la $a0 _colon_msg
li $v0 4
syscall # print ":"
lw $a0 4($sp) #
li $v0 1
syscall # print line number
li $v0 4
la $a0 _dispatch_msg
syscall # print dispatch-to-void message
li $v0 10
syscall # exit
#
# _case_abort2
#
# filename in $a0
# line number in $t1
#
# Prints error message and exits.
# Called on case on void.
#
.globl _case_abort2
_case_abort2:
sw $t1 0($sp) # save line number
addiu $sp $sp -4
addiu $a0 $a0 str_field # adjust to beginning of string
li $v0 4
syscall # print file name
la $a0 _colon_msg
li $v0 4
syscall # print ":"
lw $a0 4($sp) #
li $v0 1
syscall # print line number
li $v0 4
la $a0 _cabort_msg2
syscall # print case-on-void message
li $v0 10
syscall # exit
#
#
# _case_abort
# Is called when a case statement has no match
#
# INPUT: $a0 contains the object on which the case was
# performed
#
# Does not return!
#
.globl _case_abort
_case_abort: # $a0 contains case expression obj.
move $s0 $a0 # save the expression object
la $a0 _cabort_msg
li $v0 4
syscall # print_str
la $t1 class_nameTab
lw $v0 obj_tag($s0) # Get object tag
sll $v0 $v0 2 # *4
addu $t1 $t1 $v0
lw $t1 0($t1) # Load class name string obj.
addiu $a0 $t1 str_field # Adjust to beginning of str
li $v0 4 # print_str
syscall
la $a0 _nl
li $v0 4 # print_str
syscall
li $v0 10
syscall # Exit
#
# Copy method
#
# INPUT: $a0: object to be copied to free space in heap
#
# OUTPUT: $a0: points to the newly created copy.
#
.globl Object.copy
Object.copy:
addiu $sp $sp -8 # create stack frame
sw $ra 8($sp)
sw $a0 4($sp)
jal _MemMgr_Test # test GC area
lw $a0 4($sp) # get object size
lw $a0 obj_size($a0)
blez $a0 _objcopy_error # check for invalid size
sll $a0 $a0 2 # convert words to bytes
addiu $a0 $a0 4 # account for eyecatcher
jal _MemMgr_Alloc # allocate storage
addiu $a1 $a0 4 # pointer to new object
lw $a0 4($sp) # the self object
lw $ra 8($sp) # restore return address
addiu $sp $sp 8 # remove frame
lw $t0 obj_size($a0) # get size of object
sll $t0 $t0 2 # convert words to bytes
b _objcopy_allocated # get on with the copy
_quick_copy:
lw $t0 obj_size($a0) # get size of object to copy
blez $t0 _objcopy_error # check for invalid size
sll $t0 $t0 2 # convert words to bytes
addiu $t1 $t0 4
add $gp $gp $t1 # allocate memory
sub $a1 $gp $t0 # pointer to new object
blt $gp $s7 _objcopy_allocated # check allocation
_objcopy_allocate:
sub $gp $a1 4 # restore the original $gp
addiu $sp $sp -8 # frame size
sw $ra 8($sp) # save return address
sw $a0 4($sp) # save self
move $a0 $t1 # put bytes to allocate in $a0
jal _MemMgr_Alloc # allocate storage
addiu $a1 $a0 4 # pointer to new object
lw $a0 4($sp) # the self object
lw $ra 8($sp) # restore return address
addiu $sp $sp 8 # remove frame
lw $t0 obj_size($a0) # get size of object
sll $t0 $t0 2 # convert words to bytes
_objcopy_allocated:
addiu $t1 $0 -1
sw $t1 obj_eyecatch($a1)
add $t0 $t0 $a0 # find limit of copy
move $t1 $a1 # save source
_objcopy_loop:
lw $v0 0($a0) # copy word
sw $v0 0($t1)
addiu $a0 $a0 4 # update source
addiu $t1 $t1 4 # update destination
bne $a0 $t0 _objcopy_loop # loop
_objcopy_end:
move $a0 $a1 # put new object in $a0
jr $ra # return
_objcopy_error:
la $a0 _objcopy_msg # show error message
li $v0 4
syscall
li $v0 10 # exit
syscall
#
#
# Object.abort
#
# INPUT: $a0 contains the object on which abort() was dispatched.
#
.globl Object.abort
Object.abort:
move $s0 $a0 # save self
li $v0 4
la $a0 _abort_msg
syscall # print_str
la $t1 class_nameTab
lw $v0 obj_tag($s0) # Get object tag
sll $v0 $v0 2 # *4
addu $t1 $t1 $v0
lw $t1 0($t1) # Load class name string obj.
addiu $a0 $t1 str_field # Adjust to beginning of str
li $v0 4 # print_str
syscall
la $a0 _nl
li $v0 4
syscall # print new line
li $v0 10
syscall # Exit
#
#
# Object.type_name
#
# INPUT: $a0 object who's class name is desired
# OUTPUT: $a0 reference to class name string object
#
.globl Object.type_name
Object.type_name:
la $t1 class_nameTab
lw $v0 obj_tag($a0) # Get object tag
sll $v0 $v0 2 # *4
addu $t1 $t1 $v0 # index table
lw $a0 0($t1) # Load class name string obj.
jr $ra
#
#
# IO.out_string
#
.globl IO.out_string
IO.out_string:
addiu $sp $sp -4
sw $a0 4($sp) # save self
lw $a0 8($sp) # get arg
addiu $a0 $a0 str_field # Adjust to beginning of str
li $v0 4 # print_str
syscall
lw $a0 4($sp) # return self
addiu $sp $sp 8 # pop argument
jr $ra
#
#
# IO.out_int
#
.globl IO.out_int
IO.out_int:
addiu $sp $sp -4
sw $a0 4($sp) # save self
lw $a0 8($sp) # get arg
lw $a0 int_slot($a0) # Fetch int
li $v0 1 # print_int
syscall
lw $a0 4($sp) # return self
addiu $sp $sp 8 # pop argument
jr $ra
#
#
# IO.in_int
#
.globl IO.in_int
IO.in_int:
addiu $sp $sp -4
sw $ra 4($sp) # save return address
la $a0 Int_protObj
jal _quick_copy # Call copy
jal Int_init
addiu $sp $sp -4
sw $a0 4($sp) # save new object
li $v0, 5 # read int
syscall
lw $a0 4($sp)
addiu $sp $sp 4
sw $v0 int_slot($a0) # store int read into obj
lw $ra 4($sp)
addiu $sp $sp 4
jr $ra
#
#
# IO.in_string
#
.globl IO.in_string
IO.in_string:
addiu $sp $sp -8
sw $ra 8($sp) # save return address
sw $0 4($sp) # init GC area
jal _MemMgr_Test # test GC area
la $a0 Int_protObj # Int object for string size
jal _quick_copy
jal Int_init
sw $a0 4($sp) # save it
li $a0 str_field # size of string obj. header
addiu $a0 $a0 str_maxsize # max size of string data
jal _MemMgr_QAlloc # make sure enough room
la $a0 String_protObj # make string object
jal _quick_copy
jal String_init
lw $t0 4($sp) # get size object
sw $t0 str_size($a0) # store size object in string
sw $a0 4($sp) # save string object
addiu $gp $gp -4 # overwrite last word
_instr_ok:
li $a1 str_maxsize # largest string to read
move $a0 $gp
li $v0, 8 # read string
syscall
move $t0 $gp # t0 to beginning of string
_instr_find_end:
lb $v0 0($gp)
addiu $gp $gp 1
bnez $v0 _instr_find_end
# $gp points just after the null byte
lb $v0 0($t0) # is first byte '\0'?
bnez $v0 _instr_noteof
# we read nothing. Return '\n' (we don't have '\0'!!!)
add $v0 $zero 10 # load '\n' into $v0
sb $v0 -1($gp)
sb $zero 0($gp) # terminate
addiu $gp $gp 1
b _instr_nonl
_instr_noteof:
# Check if there really is a '\n'
lb $v0 -2($gp)
bne $v0 10 _instr_nonl
# Write '\0' over '\n'
sb $zero -2($gp) # Set end of string where '\n' was
addiu $gp $gp -1 # adjust for '\n'
_instr_nonl:
lw $a0 4($sp) # get pointer to new str obj
lw $t1 str_size($a0) # get pointer to int obj
sub $t0 $gp $a0
subu $t0 str_field # calc actual str size
addiu $t0 -1 # adjust for '\0'
sw $t0 int_slot($t1) # store string size in int obj
addi $gp $gp 3 # was already 1 past '\0'
la $t0 0xfffffffc
and $gp $gp $t0 # word align $gp
sub $t0 $gp $a0 # calc length
srl $t0 $t0 2 # divide by 4
sw $t0 obj_size($a0) # set size field of obj
lw $ra 8($sp) # restore return address
addiu $sp $sp 8
jr $ra # return
#
#
# String.length
# Returns Int Obj with string length of self
#
# INPUT: $a0 the string object
# OUTPUT: $a0 the int object which is the size of the string
#
.globl String.length
String.length:
lw $a0 str_size($a0) # fetch attr
jr $ra # Return
#
# String.concat
#
# Concatenates arg1 onto the end of self and returns a pointer
# to the new object.
#
# INPUT: $a0: the first string object (self)
# Top of stack: the second string object (arg1)
#
# OUTPUT: $a0 the new string object
#
.globl String.concat
String.concat:
addiu $sp $sp -16
sw $ra 16($sp) # save return address
sw $a0 12($sp) # save self arg.
sw $0 8($sp) # init GC area
sw $0 4($sp) # init GC area
jal _MemMgr_Test # test GC area
lw $a0 12($sp)
lw $a0 str_size($a0)
jal _quick_copy # Call copy
sw $a0 8($sp) # save new size object
lw $t1 20($sp) # load arg object
lw $t1 str_size($t1) # get size object
lw $t1 int_slot($t1) # arg string size
blez $t1 _strcat_argempty # nothing to add
lw $t0 12($sp) # load self object
lw $t0 str_size($t0) # get size object
lw $t0 int_slot($t0) # self string size
addu $t0 $t0 $t1 # new size
sw $t0 int_slot($a0) # store new size
addiu $a0 $t0 str_field # size to allocate
addiu $a0 $a0 4 # include '\0', +3 to align
la $t2 0xfffffffc
and $a0 $a0 $t2 # align on word boundary
addiu $a0 $a0 1 # make size odd for GC <-|
sw $a0 4($sp) # save size in bytes |
addiu $a0 $a0 3 # include eyecatcher(4) -1
jal _MemMgr_QAlloc # check memory
lw $a0 12($sp) # copy self
jal _quick_copy # Call copy
lw $t0 8($sp) # get the Int object
sw $t0 str_size($a0) # store it in the str obj.
sub $t1 $gp $a0 # bytes allocated by _quick_copy
lw $t0 4($sp) # get size in bytes (no eyecatcher)
sub $t0 $t0 1 # Remove extra 1 (was for GC)
sub $t1 $t0 $t1 # more memory needed
addu $gp $gp $t1 # allocate rest
srl $t0 $t0 2 # convert to words
sw $t0 obj_size($a0) # save new object size
lw $t0 12($sp) # get original self object
lw $t0 str_size($t0) # get size object
lw $t0 int_slot($t0) # self string size
addiu $t1 $a0 str_field # points to start of string data
addu $t1 $t1 $t0 # points to end: '\0'
lw $t0 20($sp) # load arg object
addiu $t2 $t0 str_field # points to start of arg data
lw $t0 str_size($t0) # get arg size
lw $t0 int_slot($t0)
addu $t0 $t0 $t2 # find limit of copy
_strcat_copy:
lb $v0 0($t2) # load from source
sb $v0 0($t1) # save in destination
addiu $t2 $t2 1 # advance each index
addiu $t1 $t1 1
bne $t2 $t0 _strcat_copy # check limit
sb $0 0($t1) # add '\0'
lw $ra 16($sp) # restore return address
addiu $sp $sp 20 # pop argument
jr $ra # return
_strcat_argempty:
lw $a0 12($sp) # load original self
lw $ra 16($sp) # restore return address
addiu $sp $sp 20 # pop argument
jr $ra # return
#
#
# String.substr(i,l)
# Returns the sub string of self from i with length l
# Offset starts at 0.
#
# INPUT: $a0 the string
# length int object on top of stack (-4)
# index int object below length on stack (-8)
# OUTPUT: The substring object in $a0
#
.globl String.substr
String.substr:
addiu $sp $sp -12 # frame
sw $ra 4($sp) # save return
sw $a0 12($sp) # save self
sw $0 8($sp) # init GC area
jal _MemMgr_Test # test GC area
lw $a0 12($sp)
lw $v0 obj_size($a0)
la $a0 Int_protObj # ask if enough room to allocate
lw $a0 obj_size($a0) # a string object, an int object,
add $a0 $a0 $v0 # and the string data
addiu $a0 $a0 2 # include 2 eyecatchers
sll $a0 $a0 2
addi $a0 $a0 str_maxsize
jal _MemMgr_QAlloc
_ss_ok:
la $a0 Int_protObj
jal _quick_copy
jal Int_init
sw $a0 8($sp) # save new length obj
la $a0 String_protObj
jal _quick_copy
jal String_init # new obj ptr in $a0
move $a2 $a0 # use a2 to make copy
addiu $gp $gp -4 # backup alloc ptr
lw $a1 12($sp) # load orig
lw $t1 20($sp) # index obj
lw $t2 16($sp) # length obj
lw $t0 str_size($a1)
lw $v1 int_slot($t1) # index
lw $v0 int_slot($t0) # size of orig
bltz $v1 _ss_abort1 # index is smaller than 0
bgt $v1 $v0 _ss_abort2 # index > orig
lw $t3 int_slot($t2) # sub length
add $v1 $v1 $t3 # index+sublength
bgt $v1 $v0 _ss_abort3
bltz $t3 _ss_abort4
lw $t4 8($sp) # load new length obj
sw $t3 int_slot($t4) # save new size
sw $t4 str_size($a0) # store size in string
lw $v1 int_slot($t1) # index
addiu $a1 $a1 str_field # advance src to str
add $a1 $a1 $v1 # advance to indexed char
addiu $a2 $a2 str_field # advance dst to str
beqz $t3 _ss_end # empty length
_ss_loop:
lb $v0 0($a1)
addiu $a1 $a1 1 # inc src
sb $v0 0($a2)
addiu $a2 $a2 1 # inc dst
addiu $t3 $t3 -1 # dec ctr
bnez $t3 _ss_loop
_ss_end:
sb $zero 0($a2) # null terminate
move $gp $a2
addiu $gp $gp 4 # realign the heap ptr
la $t0 0xfffffffc
and $gp $gp $t0 # word align $gp
sub $t0 $gp $a0 # calc object size
srl $t0 $t0 2 # div by 4
sw $t0 obj_size($a0)
lw $ra 4($sp)
addiu $sp $sp 20 # pop arguments
jr $ra
_ss_abort1:
la $a0 _sabort_msg1
b _ss_abort
_ss_abort2:
la $a0 _sabort_msg2
b _ss_abort
_ss_abort3:
la $a0 _sabort_msg3
b _ss_abort
_ss_abort4:
la $a0 _sabort_msg4
_ss_abort:
li $v0 4
syscall
la $a0 _sabort_msg
li $v0 4
syscall
li $v0 10 # exit
syscall
#MEMORY MANAGER
.globl _MemMgr_Init
_MemMgr_Init:
addiu $sp $sp -4
sw $ra 4($sp) # save return address
la $t0 _MemMgr_INITIALIZER # pointer to initialization
lw $t0 0($t0)
jalr $t0 # initialize
lw $ra 4($sp) # restore return address
addiu $sp $sp 4
jr $ra # return
#
# Memory Allocation
.globl _MemMgr_Alloc
_MemMgr_Alloc:
add $gp $gp $a0 # attempt to allocate storage
blt $gp $s7 _MemMgr_Alloc_end # check allocation
sub $gp $gp $a0 # restore $gp
addiu $sp $sp -4
sw $ra 4($sp) # save return address
move $a1 $a0 # size
addiu $a0 $sp 4 # end of stack to collect
la $t0 _MemMgr_COLLECTOR # pointer to collector function
lw $t0 0($t0)
jalr $t0 # garbage collect
lw $ra 4($sp) # restore return address
addiu $sp $sp 4
move $a0 $a1 # put size into $a0
add $gp $gp $a0 # allocate storage
_MemMgr_Alloc_end:
sub $a0 $gp $a0
jr $ra # return
#
# Query Memory Allocation
#
.globl _MemMgr_QAlloc
_MemMgr_QAlloc:
add $t0 $gp $a0 # attempt to allocate storage
blt $t0 $s7 _MemMgr_QAlloc_end # check allocation
addiu $sp $sp -4
sw $ra 4($sp) # save return address
move $a1 $a0 # size
addiu $a0 $sp 4 # end of stack to collect
la $t0 _MemMgr_COLLECTOR # pointer to collector function
lw $t0 0($t0)
jalr $t0 # garbage collect
lw $ra 4($sp) # restore return address
addiu $sp $sp 4
move $a0 $a1 # put size into $a0
_MemMgr_QAlloc_end:
jr $ra # return
#
# Test heap consistency
.globl _MemMgr_Test
_MemMgr_Test:
la $t0 _MemMgr_TEST # Check if testing enabled
lw $t0 0($t0)
beqz $t0 _MemMgr_Test_end
# Allocate 0 bytes
addiu $sp $sp -4 # Save return address
sw $ra 4($sp)
li $a1 0 # size = 0
addiu $a0 $sp 4 # end of stack to collect
la $t0 _MemMgr_COLLECTOR # pointer to collector function
lw $t0 0($t0)
jalr $t0 # garbage collect
lw $ra 4($sp) # restore return address
addiu $sp $sp 4
_MemMgr_Test_end:
jr $ra
#
# Constants
#
#
# GenGC header offsets from "heap_start"
#
GenGC_HDRSIZE=44 # size of GenGC header
GenGC_HDRL0=0 # pointers to GenGC areas
GenGC_HDRL1=4
GenGC_HDRL2=8
GenGC_HDRL3=12
GenGC_HDRL4=16
GenGC_HDRMAJOR0=20 # history of major collections
GenGC_HDRMAJOR1=24
GenGC_HDRMINOR0=28 # history of minor collections
GenGC_HDRMINOR1=32
GenGC_HDRSTK=36 # start of stack
GenGC_HDRREG=40 # current REG mask
GenGC_HEAPEXPGRAN=14
GenGC_OLDRATIO=2
GenGC_ARU_MASK=0xC37F0000
.globl _GenGC_Init
_GenGC_Init:
la $t0 heap_start
addiu $t1 $t0 GenGC_HDRSIZE
sw $t1 GenGC_HDRL0($t0) # save start of old area
sw $t1 GenGC_HDRL1($t0) # save start of reserve area
sub $t1 $a2 $t1 # find reserve/work area barrier
srl $t1 $t1 1
la $v0 0xfffffffc
and $t1 $t1 $v0
blez $t1 _GenGC_Init_error # heap initially to small
sub $gp $a2 $t1
sw $gp GenGC_HDRL2($t0) # save start of work area
sw $a2 GenGC_HDRL3($t0) # save end of work area
move $s7 $a2 # set limit pointer
sw $0 GenGC_HDRMAJOR0($t0) # clear histories
sw $0 GenGC_HDRMAJOR1($t0)
sw $0 GenGC_HDRMINOR0($t0)
sw $0 GenGC_HDRMINOR1($t0)
sw $a0 GenGC_HDRSTK($t0) # save stack start
sw $a1 GenGC_HDRREG($t0) # save register mask
li $v0 9 # get heap end
move $a0 $zero
syscall # sbrk
sw $v0 GenGC_HDRL4($t0) # save heap limit
jr $ra # return
_GenGC_Init_error:
la $a0 _GenGC_INITERROR # show error message
li $v0 4
syscall
li $v0 10 # exit
syscall
#
# Record Assignment
.globl _GenGC_Assign
_GenGC_Assign:
addiu $s7 $s7 -4
sw $a1 0($s7) # save pointer to assignment
bgt $s7 $gp _GenGC_Assign_done
addiu $sp $sp -8
sw $ra 8($sp) # save return address
sw $a0 4($sp) # sm: save $a0
move $a1 $0 # size
addiu $a0 $sp 0 # end of stack to collect
sw $0 0($sp) # play it safe with off-by-1
jal _GenGC_Collect
lw $ra 8($sp) # restore return address
lw $a0 4($sp) # restore $a0
addiu $sp $sp 8
_GenGC_Assign_done:
jr $ra # return
.globl _gc_check
_gc_check:
beqz $a1, _gc_ok # void is ok
lw $a2 obj_eyecatch($a1) # and check if it is valid
addiu $a2 $a2 1
bnez $a2 _gc_abort
_gc_ok:
jr $ra
_gc_abort:
la $a0 _gc_abort_msg
li $v0 4
syscall # print gc message
li $v0 10
syscall # exit
.globl _GenGC_Collect
_GenGC_Collect:
addiu $sp $sp -12
sw $ra 12($sp) # save return address
sw $a0 8($sp) # save stack end
sw $a1 4($sp) # save size
la $a0 _GenGC_COLLECT # print collection message
li $v0 4
syscall
lw $a0 8($sp) # restore stack end
jal _GenGC_MinorC # minor collection
la $a1 heap_start
lw $t1 GenGC_HDRMINOR1($a1)
addu $t1 $t1 $a0
srl $t1 $t1 1
sw $t1 GenGC_HDRMINOR1($a1) # update histories
sw $a0 GenGC_HDRMINOR0($a1)
move $t0 $t1 # set $t0 to max of minor
bgt $t1 $a0 _GenGC_Collect_maxmaj
move $t0 $a0
_GenGC_Collect_maxmaj:
lw $t1 GenGC_HDRMAJOR0($a1) # set $t1 to max of major
lw $t2 GenGC_HDRMAJOR1($a1)
bgt $t1 $t2 _GenGC_Collect_maxdef
move $t1 $t2
_GenGC_Collect_maxdef:
lw $t2 GenGC_HDRL3($a1)
sub $t0 $t2 $t0 # set $t0 to L3-$t0-$t1
sub $t0 $t0 $t1
lw $t1 GenGC_HDRL0($a1) # set $t1 to L3-(L3-L0)/2
sub $t1 $t2 $t1
srl $t1 $t1 1
sub $t1 $t2 $t1
blt $t0 $t1 _GenGC_Collect_breakpt # set $t0 to minimum of above
move $t0 $t1
_GenGC_Collect_breakpt:
lw $t1 GenGC_HDRL1($a1) # get end of old area
bge $t1 $t0 _GenGC_Collect_major
lw $t0 GenGC_HDRL2($a1)
lw $t1 GenGC_HDRL3($a1)
lw $t2 4($sp) # load requested size into $t2
sub $t0 $t1 $t0 # find reserve/work area barrier
srl $t0 $t0 1
la $t3 0xfffffffc
and $t0 $t0 $t3
sub $t0 $t1 $t0 # reserve/work barrier
addu $t2 $t0 $t2 # test allocation
bge $t2 $t1 _GenGC_Collect_major # check if work area too small
_GenGC_Collect_nomajor:
lw $t1 GenGC_HDRL2($a1)
sw $t1 GenGC_HDRL1($a1) # expand old area
sw $t0 GenGC_HDRL2($a1) # set new reserve/work barrier