Initial commit - work done up through 11 Feb 2020

This commit is contained in:
Gitea 2020-02-11 23:17:24 -06:00
commit 2bc7401904
27 changed files with 478 additions and 0 deletions

0
README Executable file
View file

8
arch/i386/make.config Executable file
View file

@ -0,0 +1,8 @@
ARCH_CFLAGS=
ARCH_CPPFLAGS=
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
ARCH_FREEOBJS=\
ARCH_HOSTEDOBJS=\

91
include/math.h Executable file
View file

@ -0,0 +1,91 @@
#ifndef _MATH_H
#define _MATH_H
#include <sys/types.h>
#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

14
include/stdio.h Executable file
View file

@ -0,0 +1,14 @@
#ifndef _STDIO_H
#define _STDIO_H
#include <sys/cdefs.h>
#define EOF (-1)
int printf(const char * __restrict, ...);
int putchar(int);
int puts(const char *);
#endif

9
include/stdlib.h Executable file
View file

@ -0,0 +1,9 @@
#ifndef _STDLIB_H
#define _STDLIB_H
#include <sys/cdefs.h>
__attribute__((__noreturn__))
void abort(void);
#endif

15
include/string.h Executable file
View file

@ -0,0 +1,15 @@
#ifndef _STRING_H
#define _STRING_H
#include <sys/cdefs.h>
#include <stddef.h>
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

6
include/sys/cdefs.h Executable file
View file

@ -0,0 +1,6 @@
#ifndef _SYS_CDEFS_H
#define _SYS_CDEFS_H
#define __fenix_libc 1
#endif

4
include/sys/types.h Executable file
View file

@ -0,0 +1,4 @@
#ifndef _SYS_TYPES_H
#define _SYS_TYPES_H
#endif /* not _HEADER */

92
makefile Executable file
View file

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

107
stdio/printf.c Executable file
View file

@ -0,0 +1,107 @@
#include <limits.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
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);
}

6
stdio/printf.libk.d Executable file
View file

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

16
stdio/putchar.c Executable file
View file

@ -0,0 +1,16 @@
#include <stdio.h>
#ifdef __is_libk
#include <kernel/tty.h>
#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;
}

3
stdio/putchar.libk.d Executable file
View file

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

5
stdio/puts.c Executable file
View file

@ -0,0 +1,5 @@
#include <stdio.h>
int puts(const char * string) {
return printf("%s\n", string);
}

1
stdio/puts.libk.d Executable file
View file

@ -0,0 +1 @@
stdio/puts.libk.o: stdio/puts.c include/stdio.h include/sys/cdefs.h

15
stdlib/abort.c Executable file
View file

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
__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();
}

2
stdlib/abort.libk.d Executable file
View file

@ -0,0 +1,2 @@
stdlib/abort.libk.o: stdlib/abort.c include/stdio.h include/sys/cdefs.h \
include/stdlib.h

17
string/memcmp.c Executable file
View file

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

3
string/memcmp.libk.d Executable file
View file

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

12
string/memcpy.c Executable file
View file

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

3
string/memcpy.libk.d Executable file
View file

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

20
string/memmove.c Executable file
View file

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

3
string/memmove.libk.d Executable file
View file

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

11
string/memset.c Executable file
View file

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

3
string/memset.libk.d Executable file
View file

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

9
string/strlen.c Executable file
View file

@ -0,0 +1,9 @@
#include <string.h>
size_t strlen(const char * str) {
size_t len = 0;
while(str[len] != '\0') {
len++;
}
return len;
}

3
string/strlen.libk.d Executable file
View file

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