Libc is now written in C++

This commit is contained in:
Bananymous 2022-11-14 00:27:11 +02:00
parent fd13f74bbf
commit b185ed4fd3
23 changed files with 77 additions and 87 deletions

View File

@ -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

View File

@ -2,12 +2,11 @@
#include "vga.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
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++)

View File

@ -1,16 +1,13 @@
#pragma once
#include <stddef.h>
#include <sys/cdefs.h>
#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
__END_DECLS

View File

@ -7,6 +7,9 @@
extern "C"
void kernel_main()
{
asm volatile("cli");
terminal_initialize();
printf("Hello from the kernel!\n");

View File

@ -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

View File

@ -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
__END_DECLS

View File

@ -2,13 +2,9 @@
#include <sys/cdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
__BEGIN_DECLS
__attribute__((__noreturn__))
void abort(void);
#ifdef __cplusplus
}
#endif
__END_DECLS

View File

@ -3,9 +3,7 @@
#include <stddef.h>
#include <sys/cdefs.h>
#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
__END_DECLS

View File

@ -1,3 +1,11 @@
#pragma once
#define __banan_libc 1
#define __banan_libc 1
#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

View File

@ -1,26 +1,25 @@
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
static bool print(const char* data, size_t len)
{
const unsigned char* bytes = (const unsigned char*)data;
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(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<ptrdiff_t>(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;
}

View File

@ -2,6 +2,8 @@
#if defined(__is_libk)
#include <kernel/tty.h>
#else
#include <stdlib.h>
#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;
}

View File

@ -1,17 +0,0 @@
#include <string.h>
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;
}

13
libc/string/memcmp.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <string.h>
int memcmp(const void* s1, const void* s2, size_t n)
{
const unsigned char* a = static_cast<const unsigned char*>(s1);
const unsigned char* b = static_cast<const unsigned char*>(s2);
for (size_t i = 0; i < n; i++)
if (a[i] != b[i])
return a[i] - b[i];
return 0;
}

View File

@ -1,10 +0,0 @@
#include <string.h>
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;
}

10
libc/string/memcpy.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <string.h>
void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n)
{
unsigned char* dst = static_cast<unsigned char*>(dstp);
const unsigned char* src = static_cast<const unsigned char*>(srcp);
for (size_t i = 0; i < n; i++)
dst[i] = src[i];
return dstp;
}

View File

@ -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<unsigned char*>(destp);
const unsigned char* src = static_cast<const unsigned char*>(srcp);
if (dest < src)
{

View File

@ -2,7 +2,7 @@
void* memset(void* s, int c, size_t n)
{
unsigned char* p = (unsigned char*)s;
unsigned char* p = static_cast<unsigned char*>(s);
for (size_t i = 0; i < n; i++)
p[i] = c;
return s;

View File

@ -1,6 +1,6 @@
#include <string.h>
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++)

View File

@ -1,6 +1,6 @@
#include <string.h>
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++)