forked from Bananymous/banan-os
Libc is now written in C++
This commit is contained in:
parent
fd13f74bbf
commit
b185ed4fd3
|
@ -49,25 +49,22 @@ $(ARCHDIR)/crtend.o \
|
||||||
$(ARCHDIR)/crtn.o \
|
$(ARCHDIR)/crtn.o \
|
||||||
|
|
||||||
.PHONY: all clean install install-headers install-kernel
|
.PHONY: all clean install install-headers install-kernel
|
||||||
.SUFFIXES: .o .c .cpp .S
|
.SUFFIXES: .o .cpp .S
|
||||||
|
|
||||||
all: banan-os.kernel
|
all: banan-os.kernel
|
||||||
|
|
||||||
banan-os.kernel: $(OBJS) $(ARCHDIR)/linker.ld
|
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
|
grub-file --is-x86-multiboot banan-os.kernel
|
||||||
|
|
||||||
$(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
|
$(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
|
||||||
OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@
|
OBJ=`$(CXX) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@
|
||||||
|
|
||||||
.c.o:
|
|
||||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
|
|
||||||
|
|
||||||
.cpp.o:
|
.cpp.o:
|
||||||
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||||
|
|
||||||
.S.o:
|
.S.o:
|
||||||
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f banan-os.kernel
|
rm -f banan-os.kernel
|
||||||
|
|
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
#include "vga.h"
|
#include "vga.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
static const size_t VGA_WIDTH = 80;
|
static constexpr size_t VGA_WIDTH = 80;
|
||||||
static const size_t VGA_HEIGHT = 25;
|
static constexpr size_t VGA_HEIGHT = 25;
|
||||||
static uint16_t* const VGA_MEMORY = (uint16_t*)0xC03FF000;
|
static uint16_t* const VGA_MEMORY = (uint16_t*)0xC03FF000;
|
||||||
|
|
||||||
static size_t terminal_row;
|
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);
|
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 y = 0; y < VGA_HEIGHT; y++)
|
||||||
for (size_t x = 0; x < VGA_WIDTH; x++)
|
for (size_t x = 0; x < VGA_WIDTH; x++)
|
|
@ -1,16 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__BEGIN_DECLS
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void terminal_initialize(void);
|
void terminal_initialize();
|
||||||
void terminal_putchar(char c);
|
void terminal_putchar(char c);
|
||||||
void terminal_write(const char* data, size_t size);
|
void terminal_write(const char* data, size_t size);
|
||||||
void terminal_writestring(const char* data);
|
void terminal_writestring(const char* data);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__END_DECLS
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -7,6 +7,9 @@
|
||||||
extern "C"
|
extern "C"
|
||||||
void kernel_main()
|
void kernel_main()
|
||||||
{
|
{
|
||||||
|
asm volatile("cli");
|
||||||
|
|
||||||
|
|
||||||
terminal_initialize();
|
terminal_initialize();
|
||||||
|
|
||||||
printf("Hello from the kernel!\n");
|
printf("Hello from the kernel!\n");
|
||||||
|
|
|
@ -54,7 +54,7 @@ LIBK_OBJS=$(FREEOBJS:.o=.libk.o)
|
||||||
BINARIES=libk.a
|
BINARIES=libk.a
|
||||||
|
|
||||||
.PHONY: all clean install install-headers install-libs
|
.PHONY: all clean install install-headers install-libs
|
||||||
.SUFFIXES: .o .libk.o .c .S
|
.SUFFIXES: .o .libk.o .cpp .S
|
||||||
|
|
||||||
all: $(BINARIES)
|
all: $(BINARIES)
|
||||||
|
|
||||||
|
@ -64,17 +64,17 @@ libc.a: $(OBJS)
|
||||||
libk.a: $(LIBK_OBJS)
|
libk.a: $(LIBK_OBJS)
|
||||||
$(AR) rcs $@ $(LIBK_OBJS)
|
$(AR) rcs $@ $(LIBK_OBJS)
|
||||||
|
|
||||||
.c.o:
|
.cpp.o:
|
||||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||||
|
|
||||||
.S.o:
|
.S.o:
|
||||||
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||||
|
|
||||||
.c.libk.o:
|
.cpp.libk.o:
|
||||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||||
|
|
||||||
.S.libk.o:
|
.S.libk.o:
|
||||||
$(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(BINARIES) *.a
|
rm -f $(BINARIES) *.a
|
||||||
|
|
|
@ -4,14 +4,10 @@
|
||||||
|
|
||||||
#define EOF (-1)
|
#define EOF (-1)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__BEGIN_DECLS
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int printf(const char* __restrict, ...);
|
int printf(const char* __restrict, ...);
|
||||||
int putchar(int);
|
int putchar(int);
|
||||||
int puts(const char*);
|
int puts(const char*);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__END_DECLS
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -2,13 +2,9 @@
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__BEGIN_DECLS
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
__attribute__((__noreturn__))
|
__attribute__((__noreturn__))
|
||||||
void abort(void);
|
void abort(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__END_DECLS
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -3,9 +3,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__BEGIN_DECLS
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int memcmp(const void*, const void*, size_t);
|
int memcmp(const void*, const void*, size_t);
|
||||||
void* memcpy(void* __restrict, const void* __restrict, 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* strcpy(char* __restrict, const char* __restrict);
|
||||||
char* strncpy(char* __restrict, const char* __restrict, size_t);
|
char* strncpy(char* __restrict, const char* __restrict, size_t);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
__END_DECLS
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -1,3 +1,11 @@
|
||||||
#pragma once
|
#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
|
|
@ -1,26 +1,25 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static bool print(const char* data, size_t len)
|
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++)
|
for(size_t i = 0; i < len; i++)
|
||||||
if (putchar(bytes[i]) == EOF)
|
if (putchar(bytes[i]) == EOF)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
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 (value == -2147483648)
|
||||||
{
|
{
|
||||||
if (!print("-2147483648", 11))
|
if (!print("-2147483648", 11))
|
||||||
return false;
|
return false;
|
||||||
*out_len = 11;
|
out_len = 11;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ static bool print_int(int value, size_t* out_len)
|
||||||
if (!print(ptr, len))
|
if (!print(ptr, len))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
*out_len = len;
|
out_len = len;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,9 +62,9 @@ static char bits_to_hex(unsigned char bits)
|
||||||
return bits - 10 + 'a';
|
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];
|
char buffer[2 + sizeof(ptrdiff_t) * 2];
|
||||||
buffer[0] = '0';
|
buffer[0] = '0';
|
||||||
|
@ -83,11 +82,11 @@ static bool print_ptr(void* ptr, size_t* out_len)
|
||||||
if (!print(buffer, 2 + bytes * 2))
|
if (!print(buffer, 2 + bytes * 2))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
*out_len = 2 + bytes * 2;
|
out_len = 2 + bytes * 2;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int printf(const char* restrict format, ...)
|
int printf(const char* __restrict format, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
@ -140,7 +139,7 @@ int printf(const char* restrict format, ...)
|
||||||
format++;
|
format++;
|
||||||
int value = va_arg(args, int);
|
int value = va_arg(args, int);
|
||||||
size_t len;
|
size_t len;
|
||||||
if (!print_int(value, &len))
|
if (!print_int(value, len))
|
||||||
return -1;
|
return -1;
|
||||||
written += len;
|
written += len;
|
||||||
}
|
}
|
||||||
|
@ -149,7 +148,7 @@ int printf(const char* restrict format, ...)
|
||||||
format++;
|
format++;
|
||||||
void* const ptr = va_arg(args, void*);
|
void* const ptr = va_arg(args, void*);
|
||||||
size_t len;
|
size_t len;
|
||||||
if (!print_ptr(ptr, &len))
|
if (!print_ptr(ptr, len))
|
||||||
return -1;
|
return -1;
|
||||||
written += len;
|
written += len;
|
||||||
}
|
}
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#if defined(__is_libk)
|
#if defined(__is_libk)
|
||||||
#include <kernel/tty.h>
|
#include <kernel/tty.h>
|
||||||
|
#else
|
||||||
|
#include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int putchar(int c)
|
int putchar(int c)
|
||||||
|
@ -10,6 +12,7 @@ int putchar(int c)
|
||||||
char ch = (char)c;
|
char ch = (char)c;
|
||||||
terminal_write(&ch, sizeof(ch));
|
terminal_write(&ch, sizeof(ch));
|
||||||
#else
|
#else
|
||||||
|
abort();
|
||||||
#endif
|
#endif
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
|
@ -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;
|
|
||||||
}
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
void* memmove(void* destp, const void* srcp, size_t n)
|
void* memmove(void* destp, const void* srcp, size_t n)
|
||||||
{
|
{
|
||||||
unsigned char* dest = (unsigned char*)destp;
|
unsigned char* dest = static_cast<unsigned char*>(destp);
|
||||||
const unsigned char* src = (const unsigned char*)srcp;
|
const unsigned char* src = static_cast<const unsigned char*>(srcp);
|
||||||
|
|
||||||
if (dest < src)
|
if (dest < src)
|
||||||
{
|
{
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
void* memset(void* s, int c, size_t n)
|
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++)
|
for (size_t i = 0; i < n; i++)
|
||||||
p[i] = c;
|
p[i] = c;
|
||||||
return s;
|
return s;
|
|
@ -1,6 +1,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char* strcpy(char* restrict dest, const char* restrict src)
|
char* strcpy(char* __restrict dest, const char* __restrict src)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; src[i]; i++)
|
for (i = 0; src[i]; i++)
|
|
@ -1,6 +1,6 @@
|
||||||
#include <string.h>
|
#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;
|
size_t i;
|
||||||
for (i = 0; src[i] && i < n; i++)
|
for (i = 0; src[i] && i < n; i++)
|
Loading…
Reference in New Issue