glue code for lauxlib written
I ended up writing all the file and stirng functions required for making lauxlib compile.alentours-dev
parent
8d01b60ba2
commit
d8aab40557
|
@ -22,7 +22,7 @@ LD:=$(ENV)/$(TARGET)-ld
|
|||
# -O2 sets -foptimize-sibling-calls which breaks code...
|
||||
CFLAGS:=-m64 -mcmodel=large -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs -std=c99 -g
|
||||
CFLAGS+=-I ./include -Wno-packed-bitfield-compat -O2 -fno-optimize-sibling-calls -mcmodel=kernel
|
||||
CFLAGS+= -mno-red-zone -ffreestanding
|
||||
CFLAGS+= -mno-red-zone -ffreestanding -I ./include/Lua -I ./src/Lua
|
||||
|
||||
CXFLAGS:= -Wall -Werror -nostdlib -fno-builtin -nostartfiles -I ./include
|
||||
CXFLAGS+= -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
|
||||
|
@ -76,7 +76,7 @@ Lua: $(LUA)
|
|||
|
||||
kernel.bin: version-gen Tier0 Lua
|
||||
@echo "[i] Linking kernel.bin..."
|
||||
@$(LD) -T src/kernel.ld -o kernel.bin $(TIER0) $(TIER1) $(LUA)
|
||||
@$(LD) -T src/kernel.ld -o kernel.bin $(TIER0) $(TIER1) -Lsrc/Lua -llua
|
||||
|
||||
hdd.img: kernel.bin
|
||||
@echo "[i] Creating HDD image..."
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
|
||||
//int printf(char *format, va_list args);
|
||||
#define BUFSIZ 1024
|
||||
#define FILE int
|
||||
int sprintf(char *str, const char *format, ...);
|
||||
typedef int FILE;
|
||||
|
||||
//int sprintf(char *str, const char *format, ...);
|
||||
int feof(FILE *stream);
|
||||
unsigned int fread(void *ptr, unsigned long long int size, unsigned long long int count, FILE *stream);
|
||||
int getc(FILE *stream);
|
||||
|
@ -25,7 +26,7 @@ unsigned long long int fwrite(const void *ptr, unsigned long long int size, unsi
|
|||
int fseek(FILE *stream, long offset, int whence);
|
||||
int ftell(FILE *stream);
|
||||
int fputs(const char *s, FILE *stream);
|
||||
int printf(const char *format, ...);
|
||||
#define printf(format, ...) fprintf(stdout, format, ##__VA_ARGS__);
|
||||
|
||||
#define EOF -1
|
||||
extern FILE *stdin;
|
||||
|
|
|
@ -31,9 +31,9 @@ PLATS= aix ansi bsd cucumber-kernel freebsd generic linux macosx mingw posix sol
|
|||
LUA_A= liblua.a
|
||||
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
|
||||
lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
|
||||
ltm.o lundump.o lvm.o lzio.o snprintf.o glue.o
|
||||
# removed mathlib, oslib, iolib, loadlib ~q3k
|
||||
LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o \
|
||||
ltm.o lundump.o lvm.o lzio.o snprintf.o glue.o lauxlib.o
|
||||
# removed mathlib, oslib, iolib, loadlib, ~q3k
|
||||
LIB_O= lbaselib.o lbitlib.o lcorolib.o ldblib.o \
|
||||
lstrlib.o ltablib.o linit.o
|
||||
BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)
|
||||
|
||||
|
|
|
@ -6,8 +6,12 @@
|
|||
#include "stdio.h"
|
||||
#include "Tier0/panic.h"
|
||||
#include "Tier0/kstdio.h"
|
||||
#include "Tier0/heap.h"
|
||||
#include "Tier0/kstdlib.h"
|
||||
#include "Tier0/kstdio.h"
|
||||
|
||||
int errno;
|
||||
#define NULL 0
|
||||
|
||||
// file descriptors for stdio...
|
||||
FILE _stdin = 0;
|
||||
|
@ -38,7 +42,7 @@ FILE *stderr = &_stderr;
|
|||
// setjmp.h implementation
|
||||
int setjmp(jmp_buf env)
|
||||
{
|
||||
PANIC("setjmp() stub.");
|
||||
//PANIC("setjmp() stub.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -48,23 +52,86 @@ void longjmp(jmp_buf env, int value)
|
|||
}
|
||||
|
||||
// stdio.h implementation
|
||||
//int printf(char *format, va_list args)
|
||||
//{
|
||||
// kprintf("lua :");
|
||||
//
|
||||
// va_list duplicate_args;
|
||||
// va_copy(duplicate_args, args);
|
||||
// int result = kprintf(format, duplicate_args);
|
||||
// va_end(duplicate_args);
|
||||
//
|
||||
// return result;
|
||||
//}
|
||||
// from snprintf.c
|
||||
int rpl_snprintf(char *str, unsigned long int size, const char *format, ...);
|
||||
int fprintf(FILE *stream, const char *format, ...)
|
||||
{
|
||||
va_list duplicate_args;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
va_copy(duplicate_args, args);
|
||||
|
||||
char Buffer[1024];
|
||||
int Result = -1;
|
||||
if (stream == stdout)
|
||||
Result = rpl_snprintf(Buffer, 1024, format, duplicate_args);
|
||||
else if (stream == stderr)
|
||||
{
|
||||
kprintf("ERR: ");
|
||||
Result = rpl_snprintf(Buffer, 1024, format, duplicate_args);
|
||||
}
|
||||
|
||||
if (Result >= 0)
|
||||
kprintf("%s", Buffer);
|
||||
|
||||
va_end(duplicate_args);
|
||||
va_end(args);
|
||||
return Result;
|
||||
}
|
||||
|
||||
int fflush(FILE *stream)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getc(FILE *stream)
|
||||
{
|
||||
PANIC("Lua: getc() stub.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int feof(FILE *stream)
|
||||
{
|
||||
#warning feof() stub!
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ferror(FILE *stream)
|
||||
{
|
||||
#warning ferror() stub!
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
unsigned int fread(void *ptr, unsigned long long int size, unsigned long long int count, FILE *stream)
|
||||
{
|
||||
PANIC("Lua: fread() stub.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *fopen(const char *path, const char *mode)
|
||||
{
|
||||
#warning fopen() stub!
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FILE *freopen(const char *path, const char *mode, FILE *stream)
|
||||
{
|
||||
#warning freopen() stub!
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int fclose(FILE *fp)
|
||||
{
|
||||
#warning fclose() stub!
|
||||
return 0;
|
||||
}
|
||||
|
||||
// stdlib.h implementation
|
||||
void *malloc(unsigned long long int i)
|
||||
{
|
||||
PANIC("malloc() stub.");
|
||||
return 0;
|
||||
return kmalloc(i);
|
||||
}
|
||||
|
||||
int abs(int X)
|
||||
|
@ -74,10 +141,32 @@ int abs(int X)
|
|||
|
||||
void abort(void)
|
||||
{
|
||||
PANIC("abort() stub.");
|
||||
PANIC("Lua: abort() called.");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
void *realloc(void *ptr, unsigned long int size)
|
||||
{
|
||||
if (size == 0 && ptr != 0)
|
||||
{
|
||||
kfree(ptr);
|
||||
return NULL;
|
||||
}
|
||||
if (ptr == NULL)
|
||||
return kmalloc(size);
|
||||
|
||||
void *Data = kmalloc(size);
|
||||
kmemcpy(Data, ptr, size);
|
||||
kfree(ptr);
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
void free(void *ptr)
|
||||
{
|
||||
kfree(ptr);
|
||||
}
|
||||
|
||||
// string.h implementation
|
||||
const char *strchr (const char *str, int character)
|
||||
{
|
||||
|
@ -103,4 +192,65 @@ char *strpbrk(const char *s1, const char *s2)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int luai_numpow(void *L, int a, int b)
|
||||
{
|
||||
int Result = 1;
|
||||
for (int i = 0; i < b; i++)
|
||||
{
|
||||
Result *= a;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
long int strtol (const char *str, char **endptr, int base)
|
||||
{
|
||||
const char *beg = str;
|
||||
// find end of string
|
||||
while (*str >= '0' && *str <= '9')
|
||||
str++;
|
||||
str--;
|
||||
if (endptr != 0)
|
||||
*endptr = (char *)str; // shut up gcc
|
||||
|
||||
long int value = 0;
|
||||
while (str >= beg)
|
||||
{
|
||||
char v = *str - '0';
|
||||
value += v;
|
||||
value *= 10;
|
||||
|
||||
str--;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
char *strerror(int errnum)
|
||||
{
|
||||
return "Terrible error.";
|
||||
}
|
||||
|
||||
char *strstr(const char *haystack, const char *needle)
|
||||
{
|
||||
unsigned int HaystackLength = kstrlen(haystack);
|
||||
unsigned int NeedleLength = kstrlen(needle);
|
||||
|
||||
if (NeedleLength > HaystackLength)
|
||||
return NULL;
|
||||
|
||||
for (unsigned int i = 0; i <= HaystackLength - NeedleLength; i++)
|
||||
{
|
||||
char Okay = 1;
|
||||
for (unsigned int j = 0; j < NeedleLength; j++)
|
||||
{
|
||||
if (*(haystack + i + j) != *(needle + j))
|
||||
{
|
||||
Okay = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Okay)
|
||||
return (char *)haystack + i;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -399,6 +399,10 @@
|
|||
@@ lua_number2str converts a number to a string.
|
||||
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
|
||||
*/
|
||||
// lolwut
|
||||
int rpl_snprintf(char *str, size_t size, const char *format, ...);
|
||||
#define snprintf rpl_snprintf
|
||||
#define sprintf(s, ...) snprintf(s, 9001, __VA_ARGS__)
|
||||
#define LUA_NUMBER_SCAN "%i"
|
||||
#define LUA_NUMBER_FMT "%i"
|
||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
||||
|
@ -427,10 +431,10 @@
|
|||
/* the following operations need the math library */
|
||||
#if defined(lobject_c) || defined(lvm_c)
|
||||
//#include <math.h>
|
||||
//#define luai_nummod(L,a,b) ((a) - floor((a)/(b))*(b))
|
||||
#define luai_nummod(L,a,b) ((a) % (b))
|
||||
//#define luai_numpow(L,a,b) (pow(a,b))
|
||||
LUA_NUMBER luai_nummod(void *L, LUA_NUMBER a, LUA_NUMBER b);
|
||||
LUA_NUMBER luai_numpow(void *L, LUA_NUMBER a, LUA_NUMBER b);
|
||||
|
||||
#endif
|
||||
|
||||
/* these are quite standard operations */
|
||||
|
|
|
@ -19,12 +19,31 @@
|
|||
#include "Tier0/exceptions.h"
|
||||
#include "Tier0/panic.h"
|
||||
//#include "Tier0/prng.h"
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
extern u64 _start;
|
||||
extern u64 _end;
|
||||
|
||||
void kmain_newstack(void);
|
||||
|
||||
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
if (nsize == 0)
|
||||
{
|
||||
kfree(ptr);
|
||||
return NULL;
|
||||
}
|
||||
if (ptr != 0)
|
||||
{
|
||||
void *newptr = kmalloc(nsize);
|
||||
kmemcpy(newptr, ptr, nsize);
|
||||
return newptr;
|
||||
}
|
||||
|
||||
return kmalloc(nsize);;
|
||||
}
|
||||
|
||||
|
||||
// Real kernel entry point, called from loader
|
||||
void kmain(u32 LoadContextAddress)
|
||||
{
|
||||
|
@ -81,6 +100,19 @@ void kmain(u32 LoadContextAddress)
|
|||
kmain_newstack_ptr();
|
||||
}
|
||||
|
||||
int doluastring(lua_State *State, s8 *Code)
|
||||
{
|
||||
// int Buffer = luaL_loadbuffer(State, Code, kstrlen(Code), "kmain-dostring");
|
||||
luaL_loadbuffer(State, Code, kstrlen(Code), "kmain-dostring");
|
||||
// if (Buffer != LUA_OK)
|
||||
// {
|
||||
// kprintf("[e] doluastring: Could not load Lua buffer!\n");
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void kmain_newstack(void)
|
||||
{
|
||||
|
||||
|
@ -96,7 +128,7 @@ void kmain_newstack(void)
|
|||
exceptions_init_simple();
|
||||
apic_enable_lapic();
|
||||
heap_init_simple();
|
||||
|
||||
|
||||
// enable FPU/SSE...
|
||||
__asm__ volatile(
|
||||
"movq %cr0, %rax;"
|
||||
|
@ -107,13 +139,8 @@ void kmain_newstack(void)
|
|||
"orq $0x600, %rax;"
|
||||
"movq %rax, %cr4;");
|
||||
|
||||
double wat2 = 13.37;
|
||||
wat2 *= 6.66;
|
||||
kprintf("%x\n", wat2);
|
||||
|
||||
u64 wat;
|
||||
__asm__ volatile("movq %%xmm0, %0;" : "=r" (wat));
|
||||
kprintf("%x\n", wat);
|
||||
lua_State *State = lua_newstate(l_alloc, NULL);
|
||||
doluastring(State, "print 'test'");
|
||||
|
||||
for (;;) {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue