diff --git a/README b/README new file mode 100755 index 0000000..e69de29 diff --git a/arch/i386/make.config b/arch/i386/make.config new file mode 100755 index 0000000..fbb211a --- /dev/null +++ b/arch/i386/make.config @@ -0,0 +1,8 @@ +ARCH_CFLAGS= +ARCH_CPPFLAGS= +KERNEL_ARCH_CFLAGS= +KERNEL_ARCH_CPPFLAGS= + +ARCH_FREEOBJS=\ + +ARCH_HOSTEDOBJS=\ \ No newline at end of file diff --git a/include/math.h b/include/math.h new file mode 100755 index 0000000..09cc947 --- /dev/null +++ b/include/math.h @@ -0,0 +1,91 @@ +#ifndef _MATH_H +#define _MATH_H + +#include + +#if FLT_EVAL_METHOD == 1 +typedef float_t double; +typedef double_t double; +#elseif FLT_EVAL_METHOD == 2 +typedef float_t long double; +typedef double_t long double; +#else +typedef float_t float; +typedef double_t double; +#endif + +#define M_E 2.718281828 +#define M_PI 3.14159265 +#define PI 3.14159265 + +double acos(double); +float acosf(float); +long double acosl(long double); + +double asin(double); +float asinf(float); +long double asinl(long double); + +double atan(double); +float atanf(float); +long double atanl(long double); + +double atan2(double); +float atan2f(float); +long double atan2l(long double); + +double cos(double); +float cosf(float); +long double cosl(long double); + +double sin(double); +float sinf(float); +long double sinl(long double); + +double tan(double); +float tanf(float); +long double tanl(long double); + +double acosh(double); +float acoshf(float); +long double acoshl(long double); + +double asinh(double); +float asinhf(float); +long double asinhl(long double); + +double atanh(double); +float atanhf(float); +long double atanhl(long double); + +double cosh(double); +float coshf(float); +long double coshl(long double); + +double sinh(double); +float sinhf(float); +long double sinhl(long double); + +double tanh(double); +float tanhf(float); +long double tanhl(long double); + +double exp(double); +float expf(float); +long double expl(double); + +double exp2(double); +float exp2f(float); +long double exp2l(double); + +double expm1(double); +float expm1f(float); +long double expm1l(double); + +double frexp(double, int *); +float frexpf(float, int *); +long double frexpl(double, int *); + + + +#endif diff --git a/include/stdio.h b/include/stdio.h new file mode 100755 index 0000000..de88ad1 --- /dev/null +++ b/include/stdio.h @@ -0,0 +1,14 @@ +#ifndef _STDIO_H +#define _STDIO_H + +#include + +#define EOF (-1) + +int printf(const char * __restrict, ...); + +int putchar(int); + +int puts(const char *); + +#endif diff --git a/include/stdlib.h b/include/stdlib.h new file mode 100755 index 0000000..7d3a05e --- /dev/null +++ b/include/stdlib.h @@ -0,0 +1,9 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#include + +__attribute__((__noreturn__)) +void abort(void); + +#endif diff --git a/include/string.h b/include/string.h new file mode 100755 index 0000000..b991f1d --- /dev/null +++ b/include/string.h @@ -0,0 +1,15 @@ +#ifndef _STRING_H +#define _STRING_H + +#include + +#include + +int memcmp(const void *, const void *, size_t); +void * memcpy(void * __restrict, const void * __restrict, size_t); +void * memmove(void *, const void *, size_t); +void * memset(void *, int, size_t); + +size_t strlen(const char *); + +#endif diff --git a/include/sys/cdefs.h b/include/sys/cdefs.h new file mode 100755 index 0000000..5ffdc33 --- /dev/null +++ b/include/sys/cdefs.h @@ -0,0 +1,6 @@ +#ifndef _SYS_CDEFS_H +#define _SYS_CDEFS_H + +#define __fenix_libc 1 + +#endif diff --git a/include/sys/types.h b/include/sys/types.h new file mode 100755 index 0000000..4995f79 --- /dev/null +++ b/include/sys/types.h @@ -0,0 +1,4 @@ +#ifndef _SYS_TYPES_H +#define _SYS_TYPES_H + +#endif /* not _HEADER */ diff --git a/makefile b/makefile new file mode 100755 index 0000000..f02224d --- /dev/null +++ b/makefile @@ -0,0 +1,92 @@ +DEFAULT_HOST!=../default-host.sh +HOST?=DEFAULT_HOST +HOSTARCH!=../target-triplet-to-arch.sh $(HOST) + +CFLAGS?=-O2 -g +CPPFLAGS?= +LDFLAGS?= +LIBS?= + +DESTDIR?= +PREFIX?=/usr/local +EXEC_PREFIX?=$(PREFIX) +INCLUDEDIR?=$(PREFIX)/include +LIBDIR?=$(EXEC_PREFIX)/lib + +CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra +CPPFLAGS:=$(CPPFLAGS) -D__is_libc -Iinclude +LIBK_CFLAGS:=$(CFLAGS) +LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk + +ARCHDIR=arch/$(HOSTARCH) + +include $(ARCHDIR)/make.config + +CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS) +CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS) +LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS) +LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS) + +FREEOBJS=\ +$(ARCH_FREEOBJS) \ +stdio/printf.o \ +stdio/putchar.o \ +stdio/puts.o \ +stdlib/abort.o \ +string/memcmp.o \ +string/memcpy.o \ +string/memmove.o \ +string/memset.o \ +string/strlen.o \ + +HOSTEDOBJS=\ +$(ARCH_HOSTEDOBJS) \ + +OBJS=\ +$(FREEOBJS) \ +$(HOSTEDOBJS) \ + +LIBK_OBJS=$(FREEOBJS:.o=.libk.o) + +BINARIES=libk.a #Add libc.a later + +.PHONY: all clean install install-headers install-libs +.SUFFIXES: .o .libk.o .c .S + +all: $(BINARIES) + +libc.a: $(OBJS) + $(AR) rcs $@ $(OBJS) + +libk.a: $(LIBK_OBJS) + $(AR) rcs $@ $(LIBK_OBJS) + +.c.o: + $(CC) -MD -c $< -o $@ -std=c99 $(CFLAGS) $(CPPFLAGS) + +.c.S: + $(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + +.c.libk.o: + $(CC) -MD -c $< -o $@ -std=c99 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +.S.libk.o: + $(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +clean: + rm -f $(BINARIES) *.a + rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o + rm -f $(OBJS:.o=.d) $(LIBK_OBJS:.o=.d) *.d */*.d */*/*.d + +install: install-headers install-libs + +install-headers: + mkdir -p $(DESTDIR)$(INCLUDEDIR) + cp -R include/. $(DESTDIR)$(INCLUDEDIR)/. + +install-libs: $(BINARIES) + mkdir -p $(DESTDIR)$(INCLUDEDIR) + cp $(BINARIES) $(DESTDIR)$(LIBDIR) + +-include $(OBJS:.o=.d) +-include $(LIBK_OBJS:.o=.d) diff --git a/stdio/printf.c b/stdio/printf.c new file mode 100755 index 0000000..b9b31b2 --- /dev/null +++ b/stdio/printf.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include + +static bool print(const char * str, size_t len) { + const unsigned char * bytes = (const unsigned char *) str; + + for(size_t i = 0; i < len; i++) { + if(putchar(bytes[i]) == EOF) { + return false; + } + } + + return true; +} + +int printf(const char * restrict format, ...) { + va_list parameters; + va_start(parameters, format); + + int written = 0; + + while(*format != '\0') { + size_t maxrem = INT_MAX - written; + + if(format[0] != '%' || format[1] == '%') { + if(format[0] == '%') { + format++; + } + + size_t amount = 1; + + while(format[amount] && format[amount] != '%') { + amount++; + } + + if(maxrem < amount) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(format, amount)) { + return -1; + } + + format += amount; + written += amount; + continue; + } + + const char * format_begun_at = format++; + + /* TODO: Implement all format specifiers (%g, %o, &c.) */ + if(*format == 'c') { + format++; + char c = (char) va_arg(parameters, int); + + if(!maxrem) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(&c, sizeof(c))) { + return -1; + } + + written++; + } + else if(*format == 's') { + format++; + const char * str = va_arg(parameters, const char *); + size_t len = strlen(str); + + if(maxrem < len) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(str, len)) { + return -1; + } + + written += len; + } + else { + format = format_begun_at; + size_t len = strlen(format); + + if(maxrem < len) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(format, len)) { + return -1; + } + + format += len; + } + } + + va_end(parameters); + + return(written); +} diff --git a/stdio/printf.libk.d b/stdio/printf.libk.d new file mode 100755 index 0000000..eec0880 --- /dev/null +++ b/stdio/printf.libk.d @@ -0,0 +1,6 @@ +stdio/printf.libk.o: stdio/printf.c \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include-fixed/limits.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stdbool.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stdarg.h \ + include/stdio.h include/sys/cdefs.h include/string.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/stdio/putchar.c b/stdio/putchar.c new file mode 100755 index 0000000..8f95abd --- /dev/null +++ b/stdio/putchar.c @@ -0,0 +1,16 @@ +#include + +#ifdef __is_libk +#include +#endif + +int putchar(int ic) { +#ifdef __is_libk + char c = (char) ic; + term_write(&c, sizeof(c)); +#else + /* TODO: Implement write system call to do this part */ +#endif + + return ic; +} diff --git a/stdio/putchar.libk.d b/stdio/putchar.libk.d new file mode 100755 index 0000000..1560a12 --- /dev/null +++ b/stdio/putchar.libk.d @@ -0,0 +1,3 @@ +stdio/putchar.libk.o: stdio/putchar.c include/stdio.h include/sys/cdefs.h \ + /home/helmsulfrinn/Documents/projects/coding/active/fenix/sysroot/usr/include/kernel/tty.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/stdio/puts.c b/stdio/puts.c new file mode 100755 index 0000000..12d2007 --- /dev/null +++ b/stdio/puts.c @@ -0,0 +1,5 @@ +#include + +int puts(const char * string) { + return printf("%s\n", string); +} diff --git a/stdio/puts.libk.d b/stdio/puts.libk.d new file mode 100755 index 0000000..41ba5a8 --- /dev/null +++ b/stdio/puts.libk.d @@ -0,0 +1 @@ +stdio/puts.libk.o: stdio/puts.c include/stdio.h include/sys/cdefs.h diff --git a/stdlib/abort.c b/stdlib/abort.c new file mode 100755 index 0000000..0f6454d --- /dev/null +++ b/stdlib/abort.c @@ -0,0 +1,15 @@ +#include +#include + +__attribute__((__noreturn__)) +void abort(void) { +#if defined(__is_libk) + /* TODO: Add proper kernel panic */ + printf("kernel: panic: abort()\n"); +#else + /* TODO: Abnormally terminate process like by SIGABRT */ + printf("abort()\n"); +#endif + while(1) {} + __builtin_unreachable(); +} diff --git a/stdlib/abort.libk.d b/stdlib/abort.libk.d new file mode 100755 index 0000000..6cb385e --- /dev/null +++ b/stdlib/abort.libk.d @@ -0,0 +1,2 @@ +stdlib/abort.libk.o: stdlib/abort.c include/stdio.h include/sys/cdefs.h \ + include/stdlib.h diff --git a/string/memcmp.c b/string/memcmp.c new file mode 100755 index 0000000..2024b57 --- /dev/null +++ b/string/memcmp.c @@ -0,0 +1,17 @@ +#include + +int memcmp(const void * a_ptr, const void * b_ptr, size_t size) { + const unsigned char * a = (const unsigned char *) a_ptr; + const unsigned char * b = (const unsigned char *) b_ptr; + + for(size_t i = 0; i < size; i++) { + if(a[i] < b[i]) { + return -1; + } + else if(a[i] > b[i]) { + return 1; + } + } + + return 0; +} diff --git a/string/memcmp.libk.d b/string/memcmp.libk.d new file mode 100755 index 0000000..b730894 --- /dev/null +++ b/string/memcmp.libk.d @@ -0,0 +1,3 @@ +string/memcmp.libk.o: string/memcmp.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memcpy.c b/string/memcpy.c new file mode 100755 index 0000000..21acf80 --- /dev/null +++ b/string/memcpy.c @@ -0,0 +1,12 @@ +#include + +void * memcpy(void * restrict dest, const void * restrict src, size_t size) { + unsigned char * to = (unsigned char *) dest; + const unsigned char * from = (const unsigned char *) src; + + for(size_t i = 0; i < size; i++) { + to[i] = from[i]; + } + + return dest; +} diff --git a/string/memcpy.libk.d b/string/memcpy.libk.d new file mode 100755 index 0000000..3dfcb42 --- /dev/null +++ b/string/memcpy.libk.d @@ -0,0 +1,3 @@ +string/memcpy.libk.o: string/memcpy.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memmove.c b/string/memmove.c new file mode 100755 index 0000000..d9a8340 --- /dev/null +++ b/string/memmove.c @@ -0,0 +1,20 @@ +#include + +void * memmove(void * dest, const void * src, size_t size) { + unsigned char * to = (unsigned char *) dest; + const unsigned char * from = (const unsigned char *) src; + + /* We check this to make sure we don't overwrite memory */ + if(to < from) { + for(size_t i = 0; i < size; i++) { + to[i] = from[i]; + } + } + else { + for(size_t i = size; i != 0; i--) { + to[i - 1] = from[i - 1]; + } + } + + return dest; +} diff --git a/string/memmove.libk.d b/string/memmove.libk.d new file mode 100755 index 0000000..11545b8 --- /dev/null +++ b/string/memmove.libk.d @@ -0,0 +1,3 @@ +string/memmove.libk.o: string/memmove.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memset.c b/string/memset.c new file mode 100755 index 0000000..fac6e8b --- /dev/null +++ b/string/memset.c @@ -0,0 +1,11 @@ +#include + +void * memset(void * buffer, int value, size_t size) { + unsigned char * buf = (unsigned char *) buffer; + + for(size_t i = 0; i < size; i++) { + buf[i] = (unsigned char) value; + } + + return buffer; +} diff --git a/string/memset.libk.d b/string/memset.libk.d new file mode 100755 index 0000000..96a14b8 --- /dev/null +++ b/string/memset.libk.d @@ -0,0 +1,3 @@ +string/memset.libk.o: string/memset.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/strlen.c b/string/strlen.c new file mode 100755 index 0000000..db86633 --- /dev/null +++ b/string/strlen.c @@ -0,0 +1,9 @@ +#include + +size_t strlen(const char * str) { + size_t len = 0; + while(str[len] != '\0') { + len++; + } + return len; +} diff --git a/string/strlen.libk.d b/string/strlen.libk.d new file mode 100755 index 0000000..398d7db --- /dev/null +++ b/string/strlen.libk.d @@ -0,0 +1,3 @@ +string/strlen.libk.o: string/strlen.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h