Enough to build lua

This commit is contained in:
K. Lange 2018-06-25 13:15:09 +09:00
parent a283332be6
commit a404c0f0af
14 changed files with 214 additions and 4 deletions

View File

@ -23,10 +23,12 @@ TARGET_TRIPLET=i686-pc-toaru
CC=$(TARGET_TRIPLET)-gcc
AR=$(TARGET_TRIPLET)-ar
AS=$(TARGET_TRIPLET)-as
CFLAGS= -O3 -g -std=gnu99 -I. -Iapps -pipe -mmmx -msse -msse2 -fplan9-extensions -Wall -Wextra -Wno-unused-parameter
LIBC_OBJS = $(patsubst %.c,%.o,$(wildcard libc/*.c))
LIBC_OBJS += $(patsubst %.c,%.o,$(wildcard libc/*/*.c))
LIBC_OBJS += libc/setjmp.o
LC=base/lib/libc.so
APPS=$(patsubst apps/%.c,%,$(wildcard apps/*.c))
@ -119,6 +121,9 @@ crts: base/lib/crt0.o base/lib/crti.o base/lib/crtn.o | dirs
base/lib/crt%.o: libc/crt%.s
yasm -f elf -o $@ $<
libc/setjmp.o: libc/setjmp.S
$(AS) -o $@ $<
libc/%.o: libc/%.c
$(CC) $(CFLAGS) -fPIC -c -o $@ $<

BIN
base/usr/bin/lua Executable file

Binary file not shown.

View File

@ -4,6 +4,7 @@ extern int isalnum(int c);
extern int isalpha(int c);
extern int isdigit(int c);
extern int islower(int c);
extern int isprint(int c);
extern int isgraph(int c);
extern int iscntrl(int c);
extern int isgraph(int c);

View File

@ -13,3 +13,17 @@ extern double cos(double x);
double frexp(double x, int *exp);
#define HUGE_VAL (__builtin_huge_val())
/* Unimplemented, but stubbed */
extern double acos(double x);
extern double asin(double x);
extern double atan2(double y, double x);
extern double ceil(double x);
extern double cosh(double x);
extern double ldexp(double a, int exp);
extern double log(double x);
extern double log10(double x);
extern double log2(double x);
extern double sinh(double x);
extern double tan(double x);
extern double tanh(double x);

View File

@ -9,5 +9,6 @@ typedef _sig_func_ptr sighandler_t;
#define SIG_IGN ((_sig_func_ptr)1)/* Ignore action */
#define SIG_ERR ((_sig_func_ptr)-1)/* Error return */
typedef int sig_atomic_t;
extern sighandler_t signal(int signum, sighandler_t handler);

View File

@ -61,8 +61,14 @@ extern FILE * tmpfile(void);
extern int setvbuf(FILE * stream, char * buf, int mode, size_t size);
extern int remove(const char * pathname);
extern int rename(const char * oldpath, const char * newpath);
#define _IONBF 0
#define _IOLBF 1
#define _IOFBF 2
#define getc(s) fgetc(s)
extern char * tmpnam(char * s);
#define L_tmpnam 256

View File

@ -16,9 +16,13 @@ struct tm {
};
extern struct tm *localtime(const time_t *timep);
extern struct tm *gmtime(const time_t *timep);
extern size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
extern time_t time(time_t * out);
extern double difftime(time_t a, time_t b);
extern time_t mktime(struct tm *tm);
extern int clock(void);
typedef int clock_t;
extern clock_t clock(void);
#define CLOCKS_PER_SEC 1

55
libc/math/bad.c Normal file
View File

@ -0,0 +1,55 @@
/* STUB MATH LIBRARY */
double acos(double x) {
return 0.0;
}
double asin(double x) {
return 0.0;
}
double atan2(double y, double x) {
return 0.0;
}
double ceil(double x) {
return 0.0; /* extract and convert? */
}
double cosh(double x) {
return 0.0;
}
double ldexp(double a, int exp) {
double out = a;
while (exp) {
out *= 2.0;
exp--;
}
return out;
}
double log(double x) {
return 0.0;
}
double log10(double x) {
return 0.0;
}
double log2(double x) {
return 0.0;
}
double sinh(double x) {
return 0.0;
}
double tan(double x) {
return 0.0;
}
double tanh(double x) {
return 0.0;
}

64
libc/setjmp.S Normal file
View File

@ -0,0 +1,64 @@
.global setjmp
.type setjmp, STT_FUNC
setjmp:
pushl %ebp
movl %esp,%ebp
pushl %edi
movl 8(%ebp),%edi
movl %eax,0 (%edi)
movl %ebx,4 (%edi)
movl %ecx,8 (%edi)
movl %edx,12 (%edi)
movl %esi,16 (%edi)
movl -4 (%ebp),%eax
movl %eax,20 (%edi)
movl 0 (%ebp),%eax
movl %eax,24 (%edi)
movl %esp,%eax
addl $12,%eax
movl %eax,28 (%edi)
movl 4 (%ebp),%eax
movl %eax,32 (%edi)
popl %edi
movl $0,%eax
leave
ret
.global longjmp
.type longjmp, STT_FUNC
longjmp:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%edi /* get jmp_buf */
movl 12(%ebp),%eax /* store retval in j->eax */
testl %eax,%eax
jne 0f
incl %eax
0:
movl %eax,0(%edi)
movl 24(%edi),%ebp
/*__CLI */
movl 28(%edi),%esp
pushl 32(%edi)
movl 0 (%edi),%eax
movl 4 (%edi),%ebx
movl 8 (%edi),%ecx
movl 12(%edi),%edx
movl 16(%edi),%esi
movl 20(%edi),%edi
/*__STI */
ret

7
libc/stdio/remove.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include <unistd.h>
int remove(const char * pathname) {
/* TODO directories */
return unlink(pathname);
}

8
libc/stdio/rename.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
/* TODO */
int rename(const char * oldpath, const char * newpath) {
/* Unsupported */
return -1;
}

16
libc/stdio/tmpnam.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <unistd.h>
static char _internal[L_tmpnam];
char * tmpnam(char * s) {
static int tmp_id = 1;
if (!s) {
s = _internal;
}
sprintf(s, "/tmp/tmp%d.%d", getpid(), tmp_id++);
return s;
}

View File

@ -1,5 +1,5 @@
#include <time.h>
int clock(void) {
clock_t clock(void) {
return -1;
}

View File

@ -20,7 +20,7 @@ static int day_of_week(long seconds) {
return day % 7;
}
long days_in_month(int month, int year) {
static long days_in_month(int month, int year) {
switch(month) {
case 11:
return 30;
@ -48,7 +48,7 @@ long days_in_month(int month, int year) {
return 0;
}
long secs_of_month(int months, int year) {
static long secs_of_month(int months, int year) {
long days = 0;
for (int i = 1; i < months; ++i) {
days += days_in_month(months, year);
@ -124,3 +124,32 @@ struct tm *localtime(const time_t *timep) {
fprintf(stderr, "Uh, no?\n");
return (void *)0; /// uh what
}
static unsigned int secs_of_years(int years) {
unsigned int days = 0;
years += 2000;
while (years > 1969) {
days += 365;
if (year_is_leap(years)) {
days++;
}
years--;
}
return days * 86400;
}
time_t mktime(struct tm *tm) {
return
secs_of_years(tm->tm_year + 1900) +
secs_of_month(tm->tm_mon, tm->tm_year + 1900) +
(tm->tm_mday - 1) * 86400 +
(tm->tm_hour) * 3600 +
(tm->tm_min) * 60 +
(tm->tm_sec);
}
struct tm *gmtime(const time_t *timep) {
return localtime(timep);
}