You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
1.7 KiB
28 lines
1.7 KiB
# Runtime support function alloc2 (realloc).
|
|
# Prototype address is in a0.
|
|
# Number of words to allocate is in a1.
|
|
li a2, 4 # Word size in bytes
|
|
mul a2, a1, a2 # Calculate number of bytes to allocate
|
|
add a2, gp, a2 # Estimate where GP will move
|
|
bgeu a2, s11, alloc2_15 # Go to OOM handler if too large
|
|
lw t0, @.__obj_size__(a0) # Get size of object in words
|
|
mv t2, a0 # Initialize src ptr
|
|
mv t3, gp # Initialize dest ptr
|
|
alloc2_16: # Copy-loop header
|
|
lw t1, 0(t2) # Load next word from src
|
|
sw t1, 0(t3) # Store next word to dest
|
|
addi t2, t2, 4 # Increment src
|
|
addi t3, t3, 4 # Increment dest
|
|
addi t0, t0, -1 # Decrement counter
|
|
bne t0, zero, alloc2_16 # Loop if more words left to copy
|
|
mv a0, gp # Save new object's address to return
|
|
sw a1, @.__obj_size__(a0) # Set size of new object in words
|
|
# (same as requested size)
|
|
mv gp, a2 # Set next free slot in the heap
|
|
jr ra # Return to caller
|
|
alloc2_15: # OOM handler
|
|
li a0, @error_oom # Exit code for: Out of memory
|
|
la a1, STRING["Out of memory"] # Load error message as str
|
|
addi a1, a1, @.__str__ # Load address of attribute __str__
|
|
j abort # Abort
|