diff --git a/.gitignore b/.gitignore index 62e2284..3cf1bcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Log files *.log +# Variant Folder for Stdlib +.stdlib_variant + # Staging Folder staging/ diff --git a/targets/_any/bastl/include/exception b/targets/_any/bastl/include/exception index 8b6e566..3048ae8 100644 --- a/targets/_any/bastl/include/exception +++ b/targets/_any/bastl/include/exception @@ -3,7 +3,9 @@ #define BAD_APPLE_OS_EXCEPTION_INCLUDED #include +#if defined(__baos_kernel_source__) #include "os/panic.hpp" +#endif #if defined(__cpp_exceptions) #define __ba_throw throw @@ -32,7 +34,13 @@ private: public: [[noreturn]] ExceptionAbortHelper& operator=(const std::exception& ex) noexcept { +#if defined(__baos_kernel_source__) panicf("Uncaught exception with message \"%s\" at %s:%d.", ex.what(), mFile, mLine); +#else + // TODO!!! + std::abort(); + __builtin_unreachable(); +#endif } ExceptionAbortHelper& operator()(const char* file, int line) noexcept diff --git a/targets/_any/bastl/include/type_traits b/targets/_any/bastl/include/type_traits index b119bbc..042b816 100644 --- a/targets/_any/bastl/include/type_traits +++ b/targets/_any/bastl/include/type_traits @@ -85,7 +85,7 @@ struct type_identity }; template -using type_identity_t = type_identity::type; +using type_identity_t = typename type_identity::type; template struct integral_constant diff --git a/targets/_any/stdlib/SConscript b/targets/_any/stdlib/SConscript index a21c3bd..655a806 100644 --- a/targets/_any/stdlib/SConscript +++ b/targets/_any/stdlib/SConscript @@ -1,13 +1,37 @@ Import('env') -stdlib_sources = Split(''' +shared_sources = Split(''' src/assert.cpp src/stdio.cpp src/stdlib.cpp src/string.cpp ''') -env.Append(KERNEL_SOURCES = [env.File(f) for f in stdlib_sources]) +kernel_sources = shared_sources + +env.Append(KERNEL_SOURCES = [env.File(f) for f in kernel_sources]) + +### Stdlib +stdlib_env = env.Clone() +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.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'), + source = stdlib_sources +) +stdlib_env.Default(lib_stdlib) Return('env') diff --git a/targets/_any/stdlib/src/crt0.s b/targets/_any/stdlib/src/crt0.s new file mode 100644 index 0000000..e7a72e0 --- /dev/null +++ b/targets/_any/stdlib/src/crt0.s @@ -0,0 +1,30 @@ + +.section .text + +.global _start +.type _start, @function +_start: + # set up end of stack-frame list + movq $0, %rbp + pushq %rbp + pushq %rbp + movq %rsp, %rbp + + # preserve argc and argv + pushq %rsi + pushq %rdi + + # init C library + call _init + + # restore argc and argv + popq %rdi + popq %rsi + + # call main + call main + + # exit by calling exit + movl %eax, %edi # exit code (return value of main) + call exit +.size _start, . - _start diff --git a/targets/_any/stdlib/src/stdio.cpp b/targets/_any/stdlib/src/stdio.cpp index e976154..d60e1b6 100644 --- a/targets/_any/stdlib/src/stdio.cpp +++ b/targets/_any/stdlib/src/stdio.cpp @@ -2,13 +2,14 @@ #include #include +#include #include -#include "os/tty.hpp" #include "os/tools/printf_helper.hpp" #include "os/tools/ringbuffer.hpp" #if defined(__baos_kernel_source__) #include "os/serial.hpp" +#include "os/tty.hpp" #else #include "os/syscall.hpp" #endif @@ -103,10 +104,14 @@ class StdinFile : public __file public: bool underflow() noexcept override { +#if defined(__baos_kernel_source__) for (char chr : tty::readLine()) { mBuffer.append(chr); } return true; +#else + return false; // TODO!!! +#endif } }; StdinFile gStdin;