From b185ed4fd37f5eb6797d1b48f9455a1834fe6f96 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 14 Nov 2022 00:27:11 +0200 Subject: [PATCH] Libc is now written in C++ --- kernel/Makefile | 11 ++++------- kernel/arch/i386/{tty.c => tty.cpp} | 7 +++---- kernel/include/kernel/tty.h | 11 ++++------- kernel/kernel/kernel.cpp | 3 +++ kernel/kernel/{ssp.c => ssp.cpp} | 0 libc/Makefile | 14 +++++++------- libc/include/stdio.h | 8 ++------ libc/include/stdlib.h | 8 ++------ libc/include/string.h | 8 ++------ libc/include/sys/cdefs.h | 10 +++++++++- libc/stdio/{printf.c => printf.cpp} | 21 ++++++++++----------- libc/stdio/{putchar.c => putchar.cpp} | 3 +++ libc/stdio/{puts.c => puts.cpp} | 0 libc/stdlib/{abort.c => abort.cpp} | 0 libc/string/memcmp.c | 17 ----------------- libc/string/memcmp.cpp | 13 +++++++++++++ libc/string/memcpy.c | 10 ---------- libc/string/memcpy.cpp | 10 ++++++++++ libc/string/{memmove.c => memmove.cpp} | 4 ++-- libc/string/{memset.c => memset.cpp} | 2 +- libc/string/{strcpy.c => strcpy.cpp} | 2 +- libc/string/{strlen.c => strlen.cpp} | 0 libc/string/{strncpy.c => strncpy.cpp} | 2 +- 23 files changed, 77 insertions(+), 87 deletions(-) rename kernel/arch/i386/{tty.c => tty.cpp} (93%) rename kernel/kernel/{ssp.c => ssp.cpp} (100%) rename libc/stdio/{printf.c => printf.cpp} (86%) rename libc/stdio/{putchar.c => putchar.cpp} (84%) rename libc/stdio/{puts.c => puts.cpp} (100%) rename libc/stdlib/{abort.c => abort.cpp} (100%) delete mode 100644 libc/string/memcmp.c create mode 100644 libc/string/memcmp.cpp delete mode 100644 libc/string/memcpy.c create mode 100644 libc/string/memcpy.cpp rename libc/string/{memmove.c => memmove.cpp} (66%) rename libc/string/{memset.c => memset.cpp} (68%) rename libc/string/{strcpy.c => strcpy.cpp} (63%) rename libc/string/{strlen.c => strlen.cpp} (100%) rename libc/string/{strncpy.c => strncpy.cpp} (64%) diff --git a/kernel/Makefile b/kernel/Makefile index 5dbdeeca..b45fd5a6 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -49,25 +49,22 @@ $(ARCHDIR)/crtend.o \ $(ARCHDIR)/crtn.o \ .PHONY: all clean install install-headers install-kernel -.SUFFIXES: .o .c .cpp .S +.SUFFIXES: .o .cpp .S all: banan-os.kernel banan-os.kernel: $(OBJS) $(ARCHDIR)/linker.ld - $(CC) -T $(ARCHDIR)/linker.ld -o $@ $(CFLAGS) $(LINK_LIST) + $(CXX) -T $(ARCHDIR)/linker.ld -o $@ $(CFLAGS) $(CPPFLAGS) $(LINK_LIST) grub-file --is-x86-multiboot banan-os.kernel $(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o: - OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@ - -.c.o: - $(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS) + OBJ=`$(CXX) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@ .cpp.o: $(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) .S.o: - $(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + $(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) clean: rm -f banan-os.kernel diff --git a/kernel/arch/i386/tty.c b/kernel/arch/i386/tty.cpp similarity index 93% rename from kernel/arch/i386/tty.c rename to kernel/arch/i386/tty.cpp index d4fa3008..f7f5ffb9 100644 --- a/kernel/arch/i386/tty.c +++ b/kernel/arch/i386/tty.cpp @@ -2,12 +2,11 @@ #include "vga.h" -#include #include #include -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; +static constexpr size_t VGA_WIDTH = 80; +static constexpr size_t VGA_HEIGHT = 25; static uint16_t* const VGA_MEMORY = (uint16_t*)0xC03FF000; static size_t terminal_row; @@ -21,7 +20,7 @@ void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) terminal_buffer[index] = vga_entry(c, color); } -void terminal_clear(void) +void terminal_clear() { for (size_t y = 0; y < VGA_HEIGHT; y++) for (size_t x = 0; x < VGA_WIDTH; x++) diff --git a/kernel/include/kernel/tty.h b/kernel/include/kernel/tty.h index a7715e62..604527a5 100644 --- a/kernel/include/kernel/tty.h +++ b/kernel/include/kernel/tty.h @@ -1,16 +1,13 @@ #pragma once #include +#include -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS -void terminal_initialize(void); +void terminal_initialize(); void terminal_putchar(char c); void terminal_write(const char* data, size_t size); void terminal_writestring(const char* data); -#ifdef __cplusplus -} -#endif \ No newline at end of file +__END_DECLS \ No newline at end of file diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 6be9df6c..fb2ec33e 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -7,6 +7,9 @@ extern "C" void kernel_main() { + asm volatile("cli"); + + terminal_initialize(); printf("Hello from the kernel!\n"); diff --git a/kernel/kernel/ssp.c b/kernel/kernel/ssp.cpp similarity index 100% rename from kernel/kernel/ssp.c rename to kernel/kernel/ssp.cpp diff --git a/libc/Makefile b/libc/Makefile index ae2c2fc7..840bae5c 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -54,7 +54,7 @@ LIBK_OBJS=$(FREEOBJS:.o=.libk.o) BINARIES=libk.a .PHONY: all clean install install-headers install-libs -.SUFFIXES: .o .libk.o .c .S +.SUFFIXES: .o .libk.o .cpp .S all: $(BINARIES) @@ -64,17 +64,17 @@ libc.a: $(OBJS) libk.a: $(LIBK_OBJS) $(AR) rcs $@ $(LIBK_OBJS) -.c.o: - $(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS) +.cpp.o: + $(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) .S.o: - $(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + $(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) -.c.libk.o: - $(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) +.cpp.libk.o: + $(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) .S.libk.o: - $(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + $(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) clean: rm -f $(BINARIES) *.a diff --git a/libc/include/stdio.h b/libc/include/stdio.h index c8482f8b..123cf707 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -4,14 +4,10 @@ #define EOF (-1) -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS int printf(const char* __restrict, ...); int putchar(int); int puts(const char*); -#ifdef __cplusplus -} -#endif \ No newline at end of file +__END_DECLS \ No newline at end of file diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 61c8a92b..ed30a680 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -2,13 +2,9 @@ #include -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS __attribute__((__noreturn__)) void abort(void); -#ifdef __cplusplus -} -#endif \ No newline at end of file +__END_DECLS \ No newline at end of file diff --git a/libc/include/string.h b/libc/include/string.h index d05a5b9e..67a136c3 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -3,9 +3,7 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS int memcmp(const void*, const void*, size_t); void* memcpy(void* __restrict, const void* __restrict, size_t); @@ -16,6 +14,4 @@ size_t strlen(const char*); char* strcpy(char* __restrict, const char* __restrict); char* strncpy(char* __restrict, const char* __restrict, size_t); -#ifdef __cplusplus -} -#endif \ No newline at end of file +__END_DECLS \ No newline at end of file diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h index 368603d4..42958855 100644 --- a/libc/include/sys/cdefs.h +++ b/libc/include/sys/cdefs.h @@ -1,3 +1,11 @@ #pragma once -#define __banan_libc 1 \ No newline at end of file +#define __banan_libc 1 + +#ifdef __cplusplus +#define __BEGIN_DECLS extern "C" { +#define __END_DECLS } +#else +#define __BEGIN_DECLS +#define __END_DECLS +#endif \ No newline at end of file diff --git a/libc/stdio/printf.c b/libc/stdio/printf.cpp similarity index 86% rename from libc/stdio/printf.c rename to libc/stdio/printf.cpp index 27953a86..c9acb48c 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.cpp @@ -1,26 +1,25 @@ #include #include -#include #include #include #include static bool print(const char* data, size_t len) { - const unsigned char* bytes = (const unsigned char*)data; + const unsigned char* bytes = reinterpret_cast(data); for(size_t i = 0; i < len; i++) if (putchar(bytes[i]) == EOF) return false; return true; } -static bool print_int(int value, size_t* out_len) +static bool print_int(int value, size_t& out_len) { if (value == -2147483648) { if (!print("-2147483648", 11)) return false; - *out_len = 11; + out_len = 11; return true; } @@ -52,7 +51,7 @@ static bool print_int(int value, size_t* out_len) if (!print(ptr, len)) return false; - *out_len = len; + out_len = len; return true; } @@ -63,9 +62,9 @@ static char bits_to_hex(unsigned char bits) return bits - 10 + 'a'; } -static bool print_ptr(void* ptr, size_t* out_len) +static bool print_ptr(void* ptr, size_t& out_len) { - ptrdiff_t addr = (ptrdiff_t)ptr; + ptrdiff_t addr = reinterpret_cast(ptr); char buffer[2 + sizeof(ptrdiff_t) * 2]; buffer[0] = '0'; @@ -83,11 +82,11 @@ static bool print_ptr(void* ptr, size_t* out_len) if (!print(buffer, 2 + bytes * 2)) return false; - *out_len = 2 + bytes * 2; + out_len = 2 + bytes * 2; return true; } -int printf(const char* restrict format, ...) +int printf(const char* __restrict format, ...) { va_list args; va_start(args, format); @@ -140,7 +139,7 @@ int printf(const char* restrict format, ...) format++; int value = va_arg(args, int); size_t len; - if (!print_int(value, &len)) + if (!print_int(value, len)) return -1; written += len; } @@ -149,7 +148,7 @@ int printf(const char* restrict format, ...) format++; void* const ptr = va_arg(args, void*); size_t len; - if (!print_ptr(ptr, &len)) + if (!print_ptr(ptr, len)) return -1; written += len; } diff --git a/libc/stdio/putchar.c b/libc/stdio/putchar.cpp similarity index 84% rename from libc/stdio/putchar.c rename to libc/stdio/putchar.cpp index 71152d75..d473e824 100644 --- a/libc/stdio/putchar.c +++ b/libc/stdio/putchar.cpp @@ -2,6 +2,8 @@ #if defined(__is_libk) #include +#else +#include #endif int putchar(int c) @@ -10,6 +12,7 @@ int putchar(int c) char ch = (char)c; terminal_write(&ch, sizeof(ch)); #else + abort(); #endif return c; } \ No newline at end of file diff --git a/libc/stdio/puts.c b/libc/stdio/puts.cpp similarity index 100% rename from libc/stdio/puts.c rename to libc/stdio/puts.cpp diff --git a/libc/stdlib/abort.c b/libc/stdlib/abort.cpp similarity index 100% rename from libc/stdlib/abort.c rename to libc/stdlib/abort.cpp diff --git a/libc/string/memcmp.c b/libc/string/memcmp.c deleted file mode 100644 index 7617a848..00000000 --- a/libc/string/memcmp.c +++ /dev/null @@ -1,17 +0,0 @@ -#include - -int memcmp(const void* s1, const void* s2, size_t n) -{ - const unsigned char* a = s1; - const unsigned char* b = s2; - - while (n--) - { - if (*a != *b) - return *a - *b; - a++; - b++; - } - - return 0; -} diff --git a/libc/string/memcmp.cpp b/libc/string/memcmp.cpp new file mode 100644 index 00000000..8e52d0c5 --- /dev/null +++ b/libc/string/memcmp.cpp @@ -0,0 +1,13 @@ +#include + +int memcmp(const void* s1, const void* s2, size_t n) +{ + const unsigned char* a = static_cast(s1); + const unsigned char* b = static_cast(s2); + + for (size_t i = 0; i < n; i++) + if (a[i] != b[i]) + return a[i] - b[i]; + + return 0; +} diff --git a/libc/string/memcpy.c b/libc/string/memcpy.c deleted file mode 100644 index c03360d3..00000000 --- a/libc/string/memcpy.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -void* memcpy(void* restrict destp, const void* restrict srcp, size_t n) -{ - unsigned char* dest = (unsigned char*)destp; - const unsigned char* src = (const unsigned char*)srcp; - for (size_t i = 0; i < n; i++) - dest[i] = src[i]; - return destp; -} \ No newline at end of file diff --git a/libc/string/memcpy.cpp b/libc/string/memcpy.cpp new file mode 100644 index 00000000..608caf25 --- /dev/null +++ b/libc/string/memcpy.cpp @@ -0,0 +1,10 @@ +#include + +void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n) +{ + unsigned char* dst = static_cast(dstp); + const unsigned char* src = static_cast(srcp); + for (size_t i = 0; i < n; i++) + dst[i] = src[i]; + return dstp; +} \ No newline at end of file diff --git a/libc/string/memmove.c b/libc/string/memmove.cpp similarity index 66% rename from libc/string/memmove.c rename to libc/string/memmove.cpp index d4e14350..dfd3d498 100644 --- a/libc/string/memmove.c +++ b/libc/string/memmove.cpp @@ -2,8 +2,8 @@ void* memmove(void* destp, const void* srcp, size_t n) { - unsigned char* dest = (unsigned char*)destp; - const unsigned char* src = (const unsigned char*)srcp; + unsigned char* dest = static_cast(destp); + const unsigned char* src = static_cast(srcp); if (dest < src) { diff --git a/libc/string/memset.c b/libc/string/memset.cpp similarity index 68% rename from libc/string/memset.c rename to libc/string/memset.cpp index cbad0b3f..9075d949 100644 --- a/libc/string/memset.c +++ b/libc/string/memset.cpp @@ -2,7 +2,7 @@ void* memset(void* s, int c, size_t n) { - unsigned char* p = (unsigned char*)s; + unsigned char* p = static_cast(s); for (size_t i = 0; i < n; i++) p[i] = c; return s; diff --git a/libc/string/strcpy.c b/libc/string/strcpy.cpp similarity index 63% rename from libc/string/strcpy.c rename to libc/string/strcpy.cpp index 4468d173..14fcec6d 100644 --- a/libc/string/strcpy.c +++ b/libc/string/strcpy.cpp @@ -1,6 +1,6 @@ #include -char* strcpy(char* restrict dest, const char* restrict src) +char* strcpy(char* __restrict dest, const char* __restrict src) { size_t i; for (i = 0; src[i]; i++) diff --git a/libc/string/strlen.c b/libc/string/strlen.cpp similarity index 100% rename from libc/string/strlen.c rename to libc/string/strlen.cpp diff --git a/libc/string/strncpy.c b/libc/string/strncpy.cpp similarity index 64% rename from libc/string/strncpy.c rename to libc/string/strncpy.cpp index ae52cb95..729a3c59 100644 --- a/libc/string/strncpy.c +++ b/libc/string/strncpy.cpp @@ -1,6 +1,6 @@ #include -char* strncpy(char* restrict dest, const char* restrict src, size_t n) +char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) { size_t i; for (i = 0; src[i] && i < n; i++)