Remove unused assembly functions

This commit is contained in:
2025-07-12 23:45:37 +02:00
parent 634673678a
commit c6078a17ac
2 changed files with 12 additions and 382 deletions

374
common.s
View File

@@ -2,60 +2,18 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can # v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/. # obtain one at https://mozilla.org/MPL/2.0/.
.global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower .global write_s, write_c, write_i, _print_i
.global write_s, _read_file, _write_error, write_c, write_i, _print_i .global memcmp, memchr, memmem, memcpy
.global _memcmp, _memchr, _memmem, _memcpy, _mmap
.global _current, _get, _advance, _label_counter
.global _divide_by_zero_error, _exit, _strings_index, _string_equal
.section .rodata
.equ SYS_READ, 63
.equ SYS_WRITE, 64
.equ SYS_EXIT, 93
.equ SYS_MMAP2, 222
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
.equ PROT_READ, 0x1
.equ PROT_WRITE, 0x2
.equ MAP_PRIVATE, 0x02
.equ MAP_ANONYMOUS, 0x20
new_line: .ascii "\n"
.section .text .section .text
# Write the current token to stderr. Ends the output with a newline.
#
# a0 - String pointer.
# a1 - String length.
.type _write_error, @function
_write_error:
mv t0, a0
mv t1, a1
li a0, STDERR
mv a1, t0
mv a2, t1
li a7, SYS_WRITE
ecall
li a0, STDERR
la a1, new_line
li a2, 1
li a7, SYS_WRITE
ecall
ret
# a0 - First pointer. # a0 - First pointer.
# a1 - Second pointer. # a1 - Second pointer.
# a2 - The length to compare. # a2 - The length to compare.
# #
# Returns 0 in a0 if memory regions are equal. # Returns 0 in a0 if memory regions are equal.
.type _memcmp, @function .type memcmp, @function
_memcmp: memcmp:
mv t0, a0 mv t0, a0
li a0, 0 li a0, 0
@@ -77,104 +35,6 @@ _memcmp:
.Lmemcmp_end: .Lmemcmp_end:
ret ret
# Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0.
.type _is_upper, @function
_is_upper:
li t0, 'A' - 1
sltu t1, t0, a0 # t1 = a0 >= 'A'
sltiu t2, a0, 'Z' + 1 # t2 = a0 <= 'Z'
and a0, t1, t2 # t1 = a0 >= 'A' & a0 <= 'Z'
ret
# Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0.
.type _is_lower, @function
_is_lower:
li t0, 'a' - 1
sltu t2, t0, a0 # t2 = a0 >= 'a'
sltiu t3, a0, 'z' + 1 # t3 = a0 <= 'z'
and a0, t2, t3 # t2 = a0 >= 'a' & a0 <= 'z'
ret
# Detects if the passed character is a 7-bit alpha character or an underscore.
# The character is passed in a0.
# Sets a0 to 1 if the character is an alpha character or underscore, sets it to 0 otherwise.
.type _is_alpha, @function
_is_alpha:
# Prologue.
addi sp, sp, -16
sw ra, 12(sp)
sw s0, 8(sp)
addi s0, sp, 16
sw a0, 4(sp)
call _is_upper
sw a0, 0(sp)
lw a0, 4(sp)
call _is_lower
lw t0, 4(sp)
xori t1, t0, '_'
seqz t1, t1
lw t0, 0(sp)
or a0, a0, t0
or a0, a0, t1
# Epilogue.
lw ra, 12(sp)
lw s0, 8(sp)
addi sp, sp, 16
ret
# Detects whether the passed character is a digit
# (a value between 0 and 9).
#
# Parameters:
# a0 - Exemined value.
#
# Sets a0 to 1 if it is a digit, to 0 otherwise.
.type _is_digit, @function
_is_digit:
li t0, '0' - 1
sltu t1, t0, a0 # t1 = a0 >= '0'
sltiu t2, a0, '9' + 1 # t2 = a0 <= '9'
and a0, t1, t2
ret
.type _is_alnum, @function
_is_alnum:
# Prologue.
addi sp, sp, -16
sw ra, 12(sp)
sw s0, 8(sp)
addi s0, sp, 16
sw a0, 4(sp)
call _is_alpha
sw a0, 0(sp)
lw a0, 4(sp)
call _is_digit
lw a1, 0(sp)
or a0, a0, a1
# Epilogue.
lw ra, 12(sp)
lw s0, 8(sp)
addi sp, sp, 16
ret
# Prints a string. # Prints a string.
# #
# Parameters: # Parameters:
@@ -215,55 +75,6 @@ write_s:
addi sp, sp, 32 addi sp, sp, 32
ret ret
# Reads standard input into a buffer.
# a0 - Buffer pointer.
# a1 - Buffer size.
#
# Sets s1 to the buffer passed in a0.
#
# Returns the amount of bytes written in a0.
.type _read_file, @function
_read_file:
# Prologue.
addi sp, sp, -8
sw ra, 4(sp)
sw s0, 0(sp)
addi s0, sp, 8
mv s1, a0
li a0, STDIN
mv a2, a1
mv a1, s1
li a7, SYS_READ
ecall
# Epilogue.
lw ra, 4(sp)
lw s0, 0(sp)
addi sp, sp, 8
ret
# Terminates the program. a0 contains the return code.
#
# Parameters:
# a0 - Status code.
.type _exit, @function
_exit:
li a7, SYS_EXIT
ecall
# ret
.type _divide_by_zero_error, @function
_divide_by_zero_error:
addi a7, zero, 172 # getpid
ecall
addi a1, zero, 8 # SIGFPE
addi a7, zero, 129 # kill
ecall
ret
# Writes a number to a string buffer. # Writes a number to a string buffer.
# #
# t0 - Local buffer. # t0 - Local buffer.
@@ -311,7 +122,7 @@ _print_i:
addi a2, a2, -9 addi a2, a2, -9
sw a2, 0(sp) sw a2, 0(sp)
call _memcpy call memcpy
lw a0, 0(sp) lw a0, 0(sp)
@@ -360,14 +171,6 @@ write_c:
ecall ecall
ret ret
# a0 - Pointer to an array to get the first element.
#
# Dereferences a pointer and returns what is on the address in a0.
.type _get, @function
_get:
lw a0, (a0)
ret
# Searches for the occurences of a character in the given memory block. # Searches for the occurences of a character in the given memory block.
# #
# Parameters: # Parameters:
@@ -377,8 +180,8 @@ _get:
# #
# Sets a0 to the pointer to the found character or to null if the character # Sets a0 to the pointer to the found character or to null if the character
# doesn't occur in the memory block. # doesn't occur in the memory block.
.type _memchr, @function .type memchr, @function
_memchr: memchr:
.Lmemchr_loop: .Lmemchr_loop:
beqz a2, .Lmemchr_nil # Exit if the length is 0. beqz a2, .Lmemchr_nil # Exit if the length is 0.
@@ -407,8 +210,8 @@ _memchr:
# #
# Sets a0 to the pointer to the beginning of the substring in memory or to 0 # Sets a0 to the pointer to the beginning of the substring in memory or to 0
# if the substring doesn't occur in the block. # if the substring doesn't occur in the block.
.type _memmem, @function .type memmem, @function
_memmem: memmem:
# Prologue. # Prologue.
addi sp, sp, -24 addi sp, sp, -24
sw ra, 20(sp) sw ra, 20(sp)
@@ -432,7 +235,7 @@ _memmem:
mv a0, s1 mv a0, s1
mv a1, s3 mv a1, s3
mv a2, s4 mv a2, s4
call _memcmp call memcmp
mv t0, a0 # memcmp result. mv t0, a0 # memcmp result.
mv a0, s1 # Memory pointer for the case the substring was found. mv a0, s1 # Memory pointer for the case the substring was found.
@@ -468,8 +271,8 @@ _memmem:
# a2 - Size. # a2 - Size.
# #
# Preserves a0. # Preserves a0.
.type _memcpy, @function .type memcpy, @function
_memcpy: memcpy:
mv t0, a0 mv t0, a0
.Lmemcpy_loop: .Lmemcpy_loop:
@@ -487,156 +290,3 @@ _memcpy:
.Lmemcpy_end: .Lmemcpy_end:
mv a0, t0 mv a0, t0
ret ret
# Searches for a string in a string array.
#
# Parameters:
# a0 - Number of elements in the string array.
# a1 - String array.
# a2 - Needle length.
# a3 - Needle.
#
# Sets a0 to the 1-based index of the needle in the haystack or to 0 if the
# element could not be found.
.type _strings_index, @function
_strings_index:
# Prologue.
addi sp, sp, -32
sw ra, 28(sp)
sw s0, 24(sp)
addi s0, sp, 32
sw s1, 20(sp)
mv s1, a0
sw s2, 16(sp)
mv s2, a1
sw s3, 12(sp)
mv s3, a2
sw s4, 8(sp)
mv s4, a3
sw s5, 4(sp)
li s5, 0 # Index counter.
.Lstrings_index_loop:
addi s5, s5, 1
beqz s1, .Lstrings_index_missing
lw a2, (s2) # Read the length of the current element in the haystack.
bne a2, s3, .Lstrings_index_next # Lengths don't match, skip the iteration.
addi a0, s2, 4
mv a1, s4
call _memcmp
beqz a0, .Lstrings_index_end
.Lstrings_index_next:
# Advance the pointer, reduce the length.
lw a2, (s2)
addi s2, s2, 4
add s2, s2, a2
addi s1, s1, -1
j .Lstrings_index_loop
.Lstrings_index_missing:
li s5, 0
.Lstrings_index_end:
mv a0, s5
lw s1, 20(sp)
lw s2, 16(sp)
lw s3, 12(sp)
lw s4, 8(sp)
lw s5, 4(sp)
# Epilogue.
lw ra, 28(sp)
lw s0, 24(sp)
add sp, sp, 32
ret
# Compares two strings for equality.
#
# Parameters:
# a0 - Length of the first string.
# a1 - Pointer to the first string.
# a2 - Length of the second string.
# a3 - Pointer to the second string.
#
# Sets a0 to 1 if the string are equal, to 0 if not.
.type _string_equal, @function
_string_equal:
# Prologue.
addi sp, sp, -32
sw ra, 28(sp)
sw s0, 24(sp)
addi s0, sp, 32
# Compare string lengths.
bne a0, a2, .Lstring_equal_not_found
# If lengths match, compare the content.
mv a0, a1
mv a1, a3
# a2 is already set to the length.
call _memcmp
bnez a0, .Lstring_equal_not_found
li a0, 1
j .Lstring_equal_end
.Lstring_equal_not_found:
mv a0, zero
.Lstring_equal_end:
# Epilogue.
lw ra, 28(sp)
lw s0, 24(sp)
addi sp, sp, 32
ret
# Sets a0 to the mapping address.
.type _mmap, @function
_mmap:
li a0, 0 # Address at which to create the mapping.
li a1, 4096 # The length of the mapping.
li a2, PROT_READ | PROT_WRITE # Protection flags.
li a3, MAP_ANONYMOUS | MAP_PRIVATE # The mapping is not backed by a file.
li a4, -1 # File descriptor.
li a5, 0 # Page offset.
li a7, SYS_MMAP2
ecall
ret
# Sets the a0 to the current position in the source text (s1).
.type _current, @function
_current:
mv a0, s1
ret
# Advances the position of the source text.
#
# Parameters:
# a0 - The number of bytes to advance.
.type _advance, @function
_advance:
add s1, s1, a0
ret
# Advances the global label counter by 1 setting a0 to the previous value.
#
# Parameters:
# a0 - If it is 0, resets the counter to 1.
.type _label_counter, @function
_label_counter:
bnez a0, .Llabel_counter_advance
li s2, 0
.Llabel_counter_advance:
mv a0, s2
addi s2, s2, 1
ret

View File

@@ -220,26 +220,6 @@ begin
return property return property
end; end;
proc memcmp(lhs: Pointer, rhs: Pointer, n: Word) -> Int;
var
i: Word;
lhs_char: ^Char;
rhs_char: ^Char;
result: Int;
begin
lhs_char := cast(lhs: ^Char);
rhs_char := cast(lhs: ^Char);
result := 0;
while i < n & result = 0 do
result := lhs_char - rhs_char;
lhs_char := lhs_char + 1;
rhs_char := rhs_char + 1;
i := i + 1u
end;
return result
end;
proc skip_node(stream: ^Char, strings: ^Char) -> ^Char; proc skip_node(stream: ^Char, strings: ^Char) -> ^Char;
var var
token: Word; token: Word;