Improved SCons script for building the userspace stdlib.

This commit is contained in:
2024-02-02 22:16:02 +01:00
parent a096458bdb
commit 3cd4fe9537
7 changed files with 18 additions and 18 deletions

View File

@@ -1,16 +1,20 @@
Import('env')
shared_sources = Split('''
env['CRTI_PATH'] = env.File(f'src/crti.{env["TARGET_ARCH"]}.s').abspath
env['CRTN_PATH'] = env.File(f'src/crtn.{env["TARGET_ARCH"]}.s').abspath
env['CRT0_PATH'] = env.File(f'src/crt0.{env["TARGET_ARCH"]}.s').abspath
stdlib_sources = Split(f'''
src/assert.cpp
src/stdio.cpp
src/stdlib.cpp
src/string.cpp
src/memory.{env['TARGET_ARCH']}.s
''')
kernel_sources = shared_sources
env.Append(KERNEL_SOURCES = [env.File(f) for f in kernel_sources])
env.Append(KERNEL_SOURCES = [env.File(f) for f in stdlib_sources])
### Stdlib
stdlib_env = env.Clone()
@@ -18,20 +22,17 @@ stdlib_env['AS'] = 'x86_64-baos-elf-as'
stdlib_env['CC'] = 'x86_64-baos-elf-gcc'
stdlib_env['CXX'] = 'x86_64-baos-elf-g++'
stdlib_env['LD'] = 'x86_64-baos-elf-g++'
stdlib_env['OBJSUFFIX'] = f'.baos{stdlib_env["OBJSUFFIX"]}'
stdlib_env.Append(CXXFLAGS = ['-fno-exceptions', '-fno-rtti', '-std=c++20'])
stdlib_env.Append(CPPPATH = ['#targets/_any/bastl/include', '#targets/_any/stdlib/include', '#targets/_any/kernel/include'])
stdlib_sources = Split('''
src/crt0.s
''')
for src in shared_sources:
new_src = stdlib_env.File(f'#.stdlib_variant/{stdlib_env.File(src).name}').abspath
stdlib_env.Command(new_src, src, Copy("$TARGET", "$SOURCE"))
stdlib_sources.append(new_src)
lib_stdlib = stdlib_env.StaticLibrary(
target = stdlib_env.File('#stdlib.a'),
target = stdlib_env.File('#stdlib/stdlib.a'),
source = stdlib_sources
)
stdlib_env.Default(lib_stdlib)
crti_o = stdlib_env.Object(target = '#stdlib/crti.o', source = stdlib_env['CRTI_PATH'])
crtn_o = stdlib_env.Object(target = '#stdlib/crtn.o', source = stdlib_env['CRTN_PATH'])
crt0_o = stdlib_env.Object(target = '#stdlib/crt0.o', source = stdlib_env['CRT0_PATH'])
stdlib_env.Default([lib_stdlib, crti_o, crtn_o, crt0_o])
Return('env')

View File

@@ -0,0 +1,16 @@
/* x86_64 crti.s */
.section .init
.global _init
.type _init, @function
_init:
push %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
push %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

View File

@@ -0,0 +1,10 @@
/* x86_64 crtn.s */
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popq %rbp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popq %rbp
ret

View File

@@ -0,0 +1,66 @@
.section .text
.global memcpy
.type memcpy @function
// void* memcpy(void* dest [%rdi], void* src [%rsi], size_t count [%rdx])
//
memcpy:
movq %rdi, %rax // return value
memcpy_qwords:
cmpq $8, %rdx // check if the remaining bytes are at least 8
jl memcpy_bytes // if not copy the remaining bytes per byte
movq (%rsi), %rcx // if yes, copy using movq
movq %rcx, (%rdi)
addq $8, %rsi
addq $8, %rdi
subq $8, %rdx
jmp memcpy_qwords
memcpy_bytes:
cmpq $0, %rdx
je memcpy_end
movb (%rsi), %ch
movb %ch, (%rdi)
addq $1, %rsi
addq $1, %rdi
subq $1, %rdx
jmp memcpy_bytes
memcpy_end:
ret
.global memmove
.type memmove @function
// void* memmove(void* dest [%rdi], void* src [%rsi], size_t count [%rdx])
//
memmove:
movq %rdi, %rax // preserve dest as return value
cmpq %rsi, %rdi // check if dest > src
jg memmove_backward // if yes, do everything backwards
movq %rsi, %rcx // check if (src - dest) < 8
subq %rdi, %rcx
cmpq $8, %rdi
jl memcpy_bytes // if yes, we have to do a bytewise copy
jmp memcpy_qwords // otherwise copy whole qwords
memmove_backward: // dest > src, copy backwards
addq %rdx, %rdi // dest = dest + count
addq %rdx, %rsi // src = src + count
memmove_qwords:
cmpq $8, %rdx // check if the remaining bytes are at least 8
jl memmove_bytes // if not copy the remaining bytes per byte
movq -8(%rsi), %rcx // if yes, copy using movq
movq %rcx, -8(%rdi)
subq $8, %rsi
subq $8, %rdi
subq $8, %rdx
jmp memmove_qwords
memmove_bytes:
cmpq $0, %rdx
je memmove_end
movb (%rsi), %ch
movb %ch, (%rdi)
subq $1, %rsi
subq $1, %rdi
subq $1, %rdx
jmp memmove_bytes
memmove_end:
ret