aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2016-07-16 01:28:04 +0200
committerAlex Auvolat <alex@adnab.me>2016-07-16 01:28:04 +0200
commit59000174aa50ed6b2d24a71576d15e6a53c5be0c (patch)
tree38e0a7623f1b83c4dabb1fddfc49014e623f6456 /src/lib
parent32407e728971006ed3d0885e01c22fb66c8adc57 (diff)
downloadkogata-59000174aa50ed6b2d24a71576d15e6a53c5be0c.tar.gz
kogata-59000174aa50ed6b2d24a71576d15e6a53c5be0c.zip
Add stubs for many libc functions, and a few implemenations too
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/bam.lua3
-rw-r--r--src/lib/include/math.h41
-rw-r--r--src/lib/include/setjmp.h6
-rw-r--r--src/lib/include/signal.h6
-rw-r--r--src/lib/include/stdio.h6
-rw-r--r--src/lib/include/stdlib.h5
-rw-r--r--src/lib/include/time.h25
-rw-r--r--src/lib/libc/errno.c6
-rw-r--r--src/lib/libc/locale.c22
-rw-r--r--src/lib/libc/malloc.c4
-rw-r--r--src/lib/libc/math.c151
-rw-r--r--src/lib/libc/setjmp.s53
-rw-r--r--src/lib/libc/signal.c8
-rw-r--r--src/lib/libc/stdio.c152
-rw-r--r--src/lib/libc/stdlib.c80
-rw-r--r--src/lib/libc/time.c50
-rw-r--r--src/lib/lua/luaconf.h2
17 files changed, 581 insertions, 39 deletions
diff --git a/src/lib/bam.lua b/src/lib/bam.lua
index cef678e..44edad2 100644
--- a/src/lib/bam.lua
+++ b/src/lib/bam.lua
@@ -1,5 +1,6 @@
local function lib(name)
- local source = Collect('src/lib/' .. name .. '/*.c')
+ local source = {Collect('src/lib/' .. name .. '/*.c'),
+ Collect('src/lib/' .. name .. '/*.s')}
return Compile(user_settings, source)
end
diff --git a/src/lib/include/math.h b/src/lib/include/math.h
index 64c46e7..4a23bd8 100644
--- a/src/lib/include/math.h
+++ b/src/lib/include/math.h
@@ -10,29 +10,50 @@
#define HUGE_VALF 0
#define HUGE_VALL 0
+// Float
float fabsf(float x);
- float cosf(float x);
- float sinf(float x);
- float tanf(float x);
+float cosf(float x);
+float sinf(float x);
+float tanf(float x);
- float acosf(float x);
- float asinf(float x);
+float acosf(float x);
+float asinf(float x);
float atan2f(float y, float x);
float floorf(float x);
float ceilf(float x);
float fmodf(float x, float y);
+
float sqrtf(float x);
float logf(float x);
float log2f(float x);
float log10f(float x);
float expf(float x);
float frexpf(float x, int *exp);
- float powf(float x, float y);
-
-
-
-
+float powf(float x, float y);
+
+// Double
+double fabs(double x);
+
+double cos(double x);
+double sin(double x);
+double tan(double x);
+
+double acos(double x);
+double asin(double x);
+double atan2(double y, double x);
+
+double floor(double x);
+double ceil(double x);
+double fmod(double x, double y);
+
+double sqrt(double x);
+double log(double x);
+double log2(double x);
+double log10(double x);
+double exp(double x);
+double frexp(double x, int *exp);
+double pow(double x, double y);
/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/include/setjmp.h b/src/lib/include/setjmp.h
index 7fab8c3..42df49e 100644
--- a/src/lib/include/setjmp.h
+++ b/src/lib/include/setjmp.h
@@ -3,17 +3,13 @@
// TODO
struct _jmp_buf {
- // TODO
- int a;
+ uint32_t stuff[10]; // 40 bytes
};
typedef struct _jmp_buf jmp_buf;
int setjmp(jmp_buf env);
-//int sigsetjmp(sigjmp_buf env, int savesigs);
void longjmp(jmp_buf env, int val);
-//void siglongjmp(sigjmp_buf env, int val);
-
/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/include/signal.h b/src/lib/include/signal.h
index 297db87..4b1b58f 100644
--- a/src/lib/include/signal.h
+++ b/src/lib/include/signal.h
@@ -1,8 +1,10 @@
#pragma once
-// TODO
+#include <stdatomic.h>
+
+typedef atomic_int sig_atomic_t;
-typedef int sig_atomic_t;
+// TODO
#define SIG_DFL 0 // stupid
diff --git a/src/lib/include/stdio.h b/src/lib/include/stdio.h
index 1e75270..3914a89 100644
--- a/src/lib/include/stdio.h
+++ b/src/lib/include/stdio.h
@@ -38,7 +38,7 @@ int ferror(FILE *stream);
int fileno(FILE *stream);
-size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
+size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int fflush(FILE* f);
@@ -70,7 +70,7 @@ int fsetpos(FILE *stream, const fpos_t *pos);
#define SEEK_CUR 1
#define SEEK_END 2
-#define L_tmpnam 12
+#define L_tmpnam 128
FILE *tmpfile(void);
char *tmpnam(char *s);
@@ -79,8 +79,6 @@ int remove(const char *pathname);
-
-
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
diff --git a/src/lib/include/stdlib.h b/src/lib/include/stdlib.h
index 30da9a6..53aef80 100644
--- a/src/lib/include/stdlib.h
+++ b/src/lib/include/stdlib.h
@@ -14,9 +14,8 @@ void srand(unsigned int seed);
void abort() __attribute__((__noreturn__));
-double strtod(const char *nptr, char **endptr);
-float strtof(const char *nptr, char **endptr);
-long double strtold(const char *nptr, char **endptr);
+double strtod(const char *nptr, const char **endptr);
+float strtof(const char *nptr, const char **endptr);
char *getenv(const char *name);
diff --git a/src/lib/include/time.h b/src/lib/include/time.h
index 6529c9a..fe274bb 100644
--- a/src/lib/include/time.h
+++ b/src/lib/include/time.h
@@ -1,17 +1,20 @@
#pragma once
+#include <stdint.h>
+#include <stddef.h>
+
// TODO
struct tm {
- int tm_sec; // Seconds [0,60].
- int tm_min; // Minutes [0,59].
- int tm_hour; // Hour [0,23].
- int tm_mday; // Day of month [1,31].
- int tm_mon; // Month of year [0,11].
- int tm_year; // Years since 1900.
- int tm_wday; // Day of week [0,6] (Sunday =0).
- int tm_yday; // Day of year [0,365].
- int tm_isdst; // Daylight Savings flag.
+ int tm_sec; // Seconds [0,60].
+ int tm_min; // Minutes [0,59].
+ int tm_hour; // Hour [0,23].
+ int tm_mday; // Day of month [1,31].
+ int tm_mon; // Month of year [0,11].
+ int tm_year; // Years since 1900.
+ int tm_wday; // Day of week [0,6] (Sunday =0).
+ int tm_yday; // Day of year [0,365].
+ int tm_isdst; // Daylight Savings flag.
};
typedef int64_t time_t;
@@ -20,16 +23,12 @@ time_t time(time_t*);
double difftime(time_t time1, time_t time0);
char *asctime(const struct tm *tm);
-char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
-char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
-struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
-struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
diff --git a/src/lib/libc/errno.c b/src/lib/libc/errno.c
new file mode 100644
index 0000000..b5865fa
--- /dev/null
+++ b/src/lib/libc/errno.c
@@ -0,0 +1,6 @@
+#include <errno.h>
+
+int errno = 0;
+
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/locale.c b/src/lib/libc/locale.c
new file mode 100644
index 0000000..3c7db9e
--- /dev/null
+++ b/src/lib/libc/locale.c
@@ -0,0 +1,22 @@
+#include <locale.h>
+#include <stddef.h>
+#include <limits.h>
+
+struct lconv c_locale_lconv = {
+ ".", "", "", "", "", "", "", "",
+ "", "", CHAR_MAX, CHAR_MAX, CHAR_MAX,
+ CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
+ CHAR_MAX
+};
+
+struct lconv *localeconv() {
+ // TODO
+ return &c_locale_lconv;
+}
+
+char* setlocale(int catebory, const char* locale) {
+ // TODO
+ return NULL;
+}
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/malloc.c b/src/lib/libc/malloc.c
index cb983fd..2d43656 100644
--- a/src/lib/libc/malloc.c
+++ b/src/lib/libc/malloc.c
@@ -63,6 +63,10 @@ void* calloc(size_t nmemb, size_t sz) {
return r;
}
+void* realloc(void* ptr, size_t sz) {
+ return slab_realloc(mem_allocator, ptr, sz);
+}
+
void free(void* ptr) {
slab_free(mem_allocator, ptr);
}
diff --git a/src/lib/libc/math.c b/src/lib/libc/math.c
new file mode 100644
index 0000000..b859f04
--- /dev/null
+++ b/src/lib/libc/math.c
@@ -0,0 +1,151 @@
+#include <math.h>
+
+float fabsf(float x) {
+ // TODO
+ return 0;
+}
+
+float cosf(float x) {
+ // TODO
+ return 0;
+}
+float sinf(float x) {
+ // TODO
+ return 0;
+}
+float tanf(float x) {
+ // TODO
+ return 0;
+}
+
+float acosf(float x) {
+ // TODO
+ return 0;
+}
+float asinf(float x) {
+ // TODO
+ return 0;
+}
+float atan2f(float y, float x) {
+ // TODO
+ return 0;
+}
+
+float floorf(float x) {
+ // TODO
+ return 0;
+}
+float ceilf(float x) {
+ // TODO
+ return 0;
+}
+float fmodf(float x, float y) {
+ // TODO
+ return 0;
+}
+
+float sqrtf(float x) {
+ // TODO
+ return 0;
+}
+float logf(float x) {
+ // TODO
+ return 0;
+}
+float log2f(float x) {
+ // TODO
+ return 0;
+}
+float log10f(float x) {
+ // TODO
+ return 0;
+}
+float expf(float x) {
+ // TODO
+ return 0;
+}
+float frexpf(float x, int *exp) {
+ // TODO
+ return 0;
+}
+float powf(float x, float y) {
+ // TODO
+ return 0;
+}
+
+
+
+double fabs(double x) {
+ // TODO
+ return 0;
+}
+
+double cos(double x) {
+ // TODO
+ return 0;
+}
+double sin(double x) {
+ // TODO
+ return 0;
+}
+double tan(double x) {
+ // TODO
+ return 0;
+}
+
+double acos(double x) {
+ // TODO
+ return 0;
+}
+double asin(double x) {
+ // TODO
+ return 0;
+}
+double atan2(double y, double x) {
+ // TODO
+ return 0;
+}
+
+double floor(double x) {
+ // TODO
+ return 0;
+}
+double ceil(double x) {
+ // TODO
+ return 0;
+}
+double fmod(double x, double y) {
+ // TODO
+ return 0;
+}
+
+double sqrt(double x) {
+ // TODO
+ return 0;
+}
+double log(double x) {
+ // TODO
+ return 0;
+}
+double log2(double x) {
+ // TODO
+ return 0;
+}
+double log10(double x) {
+ // TODO
+ return 0;
+}
+double exp(double x) {
+ // TODO
+ return 0;
+}
+double frexp(double x, int *exp) {
+ // TODO
+ return 0;
+}
+double pow(double x, double y) {
+ // TODO
+ return 0;
+}
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/setjmp.s b/src/lib/libc/setjmp.s
new file mode 100644
index 0000000..164cc5b
--- /dev/null
+++ b/src/lib/libc/setjmp.s
@@ -0,0 +1,53 @@
+[GLOBAL setjmp]
+setjmp:
+ ; Store general purpose registers
+ ; (in new stack frame)
+ mov [esp+4], eax
+ mov [esp+8], ebx
+ mov [esp+12], ecx
+ mov [esp+16], edx
+ mov [esp+20], edi
+ mov [esp+24], esi
+ mov [esp+28], ebp
+ mov [esp+32], esp
+
+ ; Store flags
+ pushf
+ pop eax
+ mov [esp+36], eax
+
+ ; Store return address
+ mov eax, [esp]
+ mov [esp+40], eax
+
+ ; return 0
+ xor eax, eax
+ ret
+
+
+[GLOBAL longjmp]
+longjmp:
+ ; on previous stack, resume return address
+ mov eax, [esp+32]
+ mov ebx, [esp+40]
+ mov [eax], ebx
+
+ ; resume flags
+ mov eax, [esp+36]
+ push eax
+ popf
+
+ ; load return value in eax
+ mov eax, [esp+44]
+ ; resume geneal purpose registers, except eax/esp
+ mov ebx, [esp+8]
+ mov ecx, [esp+12]
+ mov edx, [esp+16]
+ mov edi, [esp+20]
+ mov esi, [esp+24]
+ mov ebp, [esp+28]
+
+ ; resume previous esp
+ mov esp, [esp+32]
+ ; return as if we were the setjmp call
+ ret
diff --git a/src/lib/libc/signal.c b/src/lib/libc/signal.c
new file mode 100644
index 0000000..e9037c1
--- /dev/null
+++ b/src/lib/libc/signal.c
@@ -0,0 +1,8 @@
+#include <signal.h>
+
+void (*signal(int sig, void (*func)(int)))(int) {
+ // TODO
+ return 0;
+}
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/stdio.c b/src/lib/libc/stdio.c
index 22be78e..bd6c35b 100644
--- a/src/lib/libc/stdio.c
+++ b/src/lib/libc/stdio.c
@@ -60,4 +60,156 @@ int printf(const char* fmt, ...) {
return puts(buffer);
}
+// ==================
+// BELOW IS TODO
+// ==================
+
+
+int fgetc(FILE *stream) {
+ // TODO
+ return 0;
+}
+char *fgets(char *s, int size, FILE *stream) {
+ // TODO
+ return 0;
+}
+int getc(FILE *stream) {
+ // TODO
+ return 0;
+}
+int ungetc(int c, FILE *stream) {
+ // TODO
+ return 0;
+}
+
+int fputc(int c, FILE *stream) {
+ // TODO
+ return 0;
+}
+int fputs(const char *s, FILE *stream) {
+ // TODO
+ return 0;
+}
+int putc(int c, FILE *stream) {
+ // TODO
+ return 0;
+}
+
+FILE *fopen(const char *path, const char *mode) {
+ // TODO
+ return 0;
+}
+FILE *freopen(const char *path, const char *mode, FILE *stream) {
+ // TODO
+ return 0;
+}
+
+void clearerr(FILE *stream) {
+ // TODO
+}
+int feof(FILE *stream) {
+ // TODO
+ return 0;
+}
+int ferror(FILE *stream) {
+ // TODO
+ return 0;
+}
+int fileno(FILE *stream) {
+ // TODO
+ return 0;
+}
+
+
+size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ // TODO
+ return 0;
+}
+size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ // TODO
+ return 0;
+}
+
+int fflush(FILE* f) {
+ // TODO
+ return 0;
+}
+int fclose(FILE* f) {
+ // TODO
+ return 0;
+}
+
+FILE *stdin = 0;
+FILE *stdout = 0;
+FILE *stderr = 0;
+
+void setbuf(FILE *stream, char *buf) {
+ // TODO
+}
+void setbuffer(FILE *stream, char *buf, size_t size) {
+ // TODO
+}
+void setlinebuf(FILE *stream) {
+ // TODO
+}
+int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
+ // TODO
+ return 0;
+}
+
+
+int fseek(FILE *stream, long offset, int whence) {
+ // TODO
+ return 0;
+}
+long ftell(FILE *stream) {
+ // TODO
+ return 0;
+}
+void rewind(FILE *stream) {
+ // TODO
+}
+int fgetpos(FILE *stream, fpos_t *pos) {
+ // TODO
+ return 0;
+}
+int fsetpos(FILE *stream, const fpos_t *pos) {
+ // TODO
+ return 0;
+}
+
+FILE *tmpfile(void) {
+ // TODO
+ return 0;
+}
+char *tmpnam(char *s) {
+ // TODO
+ return 0;
+}
+
+int rename(const char *old, const char *new) {
+ // TODO
+ return 0;
+}
+int remove(const char *pathname) {
+ // TODO
+ return 0;
+}
+
+
+
+int fprintf(FILE *stream, const char *format, ...) {
+ // TODO
+ return 0;
+}
+int dprintf(int fd, const char *format, ...) {
+ // TODO
+ return 0;
+}
+int sprintf(char *str, const char *format, ...) {
+ // TODO
+ return 0;
+}
+
+
/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/stdlib.c b/src/lib/libc/stdlib.c
new file mode 100644
index 0000000..d8598dd
--- /dev/null
+++ b/src/lib/libc/stdlib.c
@@ -0,0 +1,80 @@
+#include <stdlib.h>
+#include <ctype.h>
+
+#include <kogata/debug.h>
+
+int rand(void) {
+ return 0; // TODO
+}
+
+void srand(unsigned int seed) {
+ //TODO
+}
+
+void abort() {
+ PANIC("Aborted.");
+}
+
+float strtof(const char *nptr, const char **endptr) {
+ return (float)strtod(nptr, endptr);
+}
+double strtod(const char *nptr, const char **endptr) {
+ // TODO: better (inf, nan, ...)
+
+ const char* p = nptr;
+ while (isspace(*p)) p++;
+
+ double val = 0;
+ double sign = 1;
+ if (*p == '-') sign = -1;
+ if (*p == '-' || *p == '+') p++;
+ while (isdigit(*p)) {
+ val = val*10. + (double)((int)*p - '0');
+ p++;
+ }
+ if (*p == '.') {
+ p++;
+ double fac = 0.1;
+ while (isdigit(*p)) {
+ val += fac * (double)((int)*p - '0');
+ fac /= 10.;
+ p++;
+ }
+ }
+ if (*p == 'e' || *p == 'E') {
+ p++;
+ int exp = 0;
+ int sexp = 1;
+ if (*p == '-') sexp = -1;
+ if (*p == '-' || *p =='+') p++;
+ while (isdigit(*p)) {
+ exp = exp * 10 + (*p - '0');
+ p++;
+ }
+ if (sexp == 1) {
+ for (int i = 0; i < exp; i++) val *= 10;
+ } else {
+ for (int i = 0; i < exp; i++) val /= 10;
+ }
+ }
+ if (endptr != NULL) *endptr = p-1;
+
+ return val * sign;
+}
+
+char *getenv(const char *name) {
+ // TODO
+ return 0;
+}
+
+int system(const char *command) {
+ // TODO
+ return -1;
+}
+
+int abs(int j) {
+ if (j < 0) return -j;
+ return j;
+}
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/time.c b/src/lib/libc/time.c
new file mode 100644
index 0000000..c10d071
--- /dev/null
+++ b/src/lib/libc/time.c
@@ -0,0 +1,50 @@
+#include <time.h>
+
+time_t time(time_t* t) {
+ // TODO
+ return 0;
+}
+double difftime(time_t time1, time_t time0) {
+ // TODO
+ return 0;
+}
+
+char *asctime(const struct tm *tm) {
+ // TODO --
+ return 0;
+}
+
+char *ctime(const time_t *timep) {
+ // TODO --
+ return 0;
+}
+
+struct tm *gmtime(const time_t *timep) {
+ // TODO
+ return 0;
+}
+
+struct tm *localtime(const time_t *timep) {
+ // TODO
+ return 0;
+}
+
+time_t mktime(struct tm *tm) {
+ // TODO
+ return 0;
+}
+
+size_t strftime(char *s, size_t max, const char *format, const struct tm *tm) {
+ // TODO
+ return 0;
+}
+
+
+clock_t clock(void) {
+ // TODO
+ return 0;
+}
+
+
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/lua/luaconf.h b/src/lib/lua/luaconf.h
index fb6fe9c..c6592fb 100644
--- a/src/lib/lua/luaconf.h
+++ b/src/lib/lua/luaconf.h
@@ -39,7 +39,7 @@
** ensure that all software connected to Lua will be compiled with the
** same configuration.
*/
-#define LUA_32BITS
+// #define LUA_32BITS
/*