Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,30 @@
option casemap:none
strdup proto :qword
printf proto :qword, :vararg
exit proto :dword
.data
bstr db "String 1",0
.data?
buff dq ?
.code
main proc
invoke printf, CSTR("Copying %s to buff with strdup using invoke....",10), addr bstr
invoke strdup, addr bstr
mov buff, rax
invoke printf, CSTR("buff now = %s",10), buff
invoke exit, 0
ret
main endp
end
;Now, we could target a specific ABI by assigning the call values to the registers like
;.code
;main proc
; lea rdi, bstr
; call strdup
; mov buff, rax
;main endp
;end

View file

@ -0,0 +1,246 @@
option casemap:none
windows64 equ 1
linux64 equ 3
ifndef __LUA_CLASS__
__LUA_CLASS__ equ 1
LUA_OK equ 0
LUA_YEILD equ 1
LUA_ERRRUN equ 2
LUA_ERRSYNTAX equ 3
LUA_ERRMEM equ 4
;; Lua variable types - defined in lua.h
LUA_TNONE equ -1
LUA_TNIL equ 0
LUA_TBOOL equ 1
LUA_TNUMB equ 3
LUA_TSTRING equ 4
LUA_TFUNC equ 6
LUA_MULTRET equ -1
;; to pop or not to pop, that is the question..
DO_POP equ 1
NO_POP equ 0
if @Platform eq windows64
option dllimport:<kernel32>
GetProcessHeap proto
ExitProcess proto :dword
HeapAlloc proto :qword, :dword, :qword
HeapFree proto :qword, :dword, :qword
option dllimport:none
exit equ ExitProcess
elseif @Platform eq linux64
malloc proto SYSTEMV :qword
free proto SYSTEMV :qword
endif
printf proto :qword, :vararg
exit proto :dword
;; Lua.h funcs
luaL_newstate proto ;; lua_State *luaL_newstate();
lua_gettop proto :qword ;; int lua_getopt(lua_State *L);
lua_close proto :qword ;; void lua_close(lua_State *L);
luaL_openlibs proto :qword ;; int luaL_openlibs(lua_State *L);
lua_pushnil proto :qword ;; void lua_pushnil(lua_State *L);
lua_pushinteger proto :qword, :qword ;; void lua_pushinteger(lua_State *L, lua_Integer arg);
lua_settop proto :qword, :dword ;; int lua_setopt(lua_State *L, int idx);
lua_setglobal proto :qword, :qword ;; void lua_setglobal(lua_State *L, const char *var);
lua_getglobal proto :qword, :qword ;; int lua_getglobal(lua_State *L, const char *gn);
luaL_loadstring proto :qword, :qword ;; int to_loadstring(lua_state *L, const char *string);
lua_pushstring proto :qword, :qword ;; const char *pushstring(lua_State *L, const char *var);
lua_pushboolean proto :qword, :dword ;; void lua_pushboolean(lua_State *L, int b)
lua_isinteger proto :qword, :dword ;; lua_Integer lua_isinteger(lua_State *L, int idx);
lua_tointegerx proto :qword, :dword,:dword ;; lua_Integer lua_tointeger(lua_State *L, int n);
luaL_loadfilex proto :qword, :qword,:dword ;; int luaL_loadfile(lua_State *L, const char *fn, const char *m)
;; void lua_pushcclosure(lua_State *L, lua_CFunction f, int n);
lua_pushccloure proto :qword, :qword, :dword
;; int lua_pcallk(lua_State *L, int argcnt, int results, int errfunc, int context, lua_CFunction k);
lua_pcallk proto :qword, :dword, :dword, :dword, :dword, :dword
CLASS lua_class
CMETHOD run
CMETHOD loadstring
CMETHOD loadfile
CMETHOD setglobal
CMETHOD getglobal
CMETHOD getstate
ENDMETHODS
lua_state qword 0
ENDCLASS
METHOD lua_class, Init, <VOIDARG>, <>
mov rbx, thisPtr
assume rbx:ptr lua_class
invoke luaL_newstate
mov [rbx].lua_state, rax
invoke luaL_openlibs, [rbx].lua_state
.if rax != LUA_OK
invoke printf, CSTR("---> Lua failed to open libs",10)
jmp _ext
.endif
_ext:
mov rax, rbx
assume rbx:nothing
ret
ENDMETHOD
;; dopop = pop off the virtual stack.
METHOD lua_class, run, <VOIDARG>, <>, narg:dword, nret:dword, dopop:word
mov rbx, thisPtr
assume rbx:ptr lua_class
invoke lua_pcallk, [rbx].lua_state, narg, nret, 0, 0, 0
.if rax != LUA_OK
invoke printf, CSTR("--> lua_pcallk failed with %i",10), rax
.else
;; In some cases, we want to pop the top off the stack. But not always
;; so, DO_POP or NO_POP depending..
.if dopop == DO_POP
invoke lua_gettop, [rbx].lua_state
not eax
invoke lua_settop, [rbx].lua_state, eax
.endif
.endif
assume rbx:nothing
ret
ENDMETHOD
METHOD lua_class, loadstring, <VOIDARG>, <>, s:qword
invoke luaL_loadstring, [thisPtr].lua_class.lua_state, s
ret
ENDMETHOD
METHOD lua_class, loadfile, <VOIDARG>, <>, fn:qword
mov rbx, thisPtr
assume rbx:ptr lua_class
invoke luaL_loadfilex, [rbx].lua_class.lua_state, fn, 0
invoke lua_pcallk, [rbx].lua_state, 0, LUA_MULTRET, 0, 0, 0
.if rax == LUA_OK
invoke lua_gettop, [rbx].lua_state
not eax
invoke lua_settop, [rbx].lua_state, eax
.endif
mov rax, 0
assume rbx:nothing
ret
ENDMETHOD
;; lua_class.setglobals(qword ArumentVar, qword ArgumentReferenceName, dword LUA_TTYPE)
;; arg = String(char *) or boolean or integer.
;; an = argument reference name - The name used to reference arg1 from Lua code.
;; t = defined type of argument used.
METHOD lua_class, setglobal, <VOIDARG>, <>, arg:qword, an:qword, t:dword
local targ:qword
local ttype:dword
local tan:qword
mov rbx, thisPtr
assume rbx:ptr lua_class
mov targ, arg
mov ttype, t
mov tan, an
.if ttype == LUA_TNIL
invoke lua_pushnil, [rbx].lua_state
.elseif ttype == LUA_TBOOL
mov rax, targ
invoke lua_pushboolean, [rbx].lua_state, eax
.elseif ttype == LUA_TSTRING
invoke lua_pushstring, [rbx].lua_state, targ
.elseif ttype == LUA_TFUNC
;; Used for a lua function call.. But I'm lazy so, check the
;; Lua docs for info about that...
.else
;; Assumes it's an Integer type. Lua's integers are int or long sized(set in luaconfig.h).
;; so qword sized variable to be safe.
invoke lua_pushinteger, [rbx].lua_state, targ
.endif
invoke lua_setglobal, [rbx].lua_state, tan
assume rbx:nothing
ret
ENDMETHOD
METHOD lua_class, getglobal, <VOIDARG>, <>, gn:qword
invoke lua_getglobal, [thisPtr].lua_class.lua_state, gn
ret
ENDMETHOD
METHOD lua_class, getstate, <VOIDARG>, <>
mov rax, [thisPtr].lua_class.lua_state
ret
ENDMETHOD
METHOD lua_class, Destroy, <VOIDARG>, <>
mov rbx, thisPtr
assume rbx:ptr lua_class
.if [rbx].lua_state != 0
invoke lua_close, [rbx].lua_state
.endif
assume rbx:nothing
ret
ENDMETHOD
endif ;; __LUA_CLASS__
.data
d1 dq 1342
d2 dq 1551
pFile db "addition.lua",0
.code
main proc
local lvm:ptr lua_class
local state:qword
invoke printf, CSTR("-> Attempting to init Lua...",10)
mov lvm, _NEW(lua_class)
lvm->getstate()
mov state, rax
invoke printf, CSTR("-> LVM started, Using loadstring..",10)
lvm->loadstring(CSTR("print('---> Goodbye, world from Lua..')"))
lvm->run(0,0, DO_POP)
lvm->setglobal(CSTR("A string that's global"), CSTR("teststr"), LUA_TSTRING)
lvm->setglobal(12, CSTR("numb"), LUA_TNONE)
lvm->loadstring(CSTR("print('---> Global str: ' .. teststr .. '\n---> Global int: ' .. numb)"))
lvm->run(0, 0, DO_POP)
invoke printf, CSTR("-> Loading lua file...",10)
lea rax, pFile
lvm->loadfile(rax)
.if rax != LUA_OK
invoke printf, CSTR("-> Failed loadfile, returned with: %i",10), rax
jmp _ext
.endif
lvm->getglobal(CSTR("addition"))
.if rax != LUA_TFUNC
invoke printf, CSTR("-> Global wasn't a function",10,"-> Type retruned is: %d",10), rax
jmp _ext
.endif
;; We're just pushing the ints onto the Virtual stack for arguments
invoke lua_pushinteger, state, d1
invoke lua_pushinteger, state, d2
lvm->run(2,1,NO_POP)
.if rax == LUA_OK
invoke lua_isinteger, state, -1
.if rax != 1
invoke printf, CSTR("-> Value is NOT an integer..",10)
jmp _ext
.endif
invoke lua_tointegerx, state, -1, 0
push rax
;; Set the top of the virtual stack to our return value..
invoke lua_settop, state, 1
pop rax
invoke printf, CSTR("-> Return from lua func: %i",10), eax
.else
invoke printf, CSTR("-> lvm->run() failed to call func returned: %i",10), rax
.endif
_ext:
_DELETE(lvm)
mov rax, 0
invoke exit, 0
main endp
end

View file

@ -0,0 +1,4 @@
function addition(a, b)
print('---> Lua calc: ' .. a .. ' + ' .. b .. ' = ' .. a+b)
return a + b
end

View file

@ -0,0 +1,176 @@
;; libc stuff..
extern printf
extern exit
extern malloc
extern free
extern fopen
extern fclose
extern fread
extern fseek
extern ftell
extern rewind
;; Wren stuff..
extern wrenNewVM
extern wrenInterpret
extern wrenFreeVM
extern wrenGetSlotString
extern wrenSetSlotString
extern wrenInitConfiguration
%define WREN_RESULT_SUCCESS 0
%define WREN_RESULT_COMPILE_ERROR 1
%define WREN_RESULT_RUNTIME_ERROR 2
;; Stuff...
extern C_strdup
;; time saver 'macros' for PIC(mmm, PIE..)
;; These Macros basically end up being exactly what they look
;; like in code, there's very little preprocessing in NASM,
;; unlike M/UASM's macro systems.(Still more than Gas doe..)
%macro xlea 2
lea %1, [rel %2]
%endmacro
%macro xcall 1
call [rel %1 wrt ..got]
%endmacro
section .bss
wrenConfig resb 84
wrenVM resq 1
section .rodata
szmsg db "--> Starting and configuring WrenVM",10,0
sznoargs db "--> ! No args passed. Supply a filename.",10,0
sznofile db "--> ! Invaild file passed.",10,0
szothererr db "--> ! Wren Error, check script...",10,0
szmod db "main",0
pfmt db "%s",0
szread db "r",0 ;; Why does this have to be a string? Seriously.
;; Let this freakshow begin..
section .text
global main
main:
push rbp
mov rbp, rsp
sub rsp, 16
cmp edi, 1 ;; argc
jle _main_noargs ;; if(argc <= 1)
mov rax, qword [rsi+1*8] ;; argv[1] - dun dun dunnnn
mov qword [rbp-8], rax
xlea rdi, szmsg
xcall printf
xlea rdi, wrenConfig
xcall wrenInitConfiguration
xlea rax, wrenConfig
xlea rbx, bindfn
mov qword [rax+24], rbx ;; wrenconfig.WrenBindForeignFn
xlea rbx, writefn
mov qword [rax+40], rbx ;; wrenconfig.WrenWriteFn
xlea rbx, errfn
mov qword [rax+48], rbx ;; wrenconfig.WrenErrorFn
xlea rdi, wrenConfig
xcall wrenNewVM
mov [rel wrenVM], rax
mov rdi, qword [rbp-8]
call srcread
mov qword [rbp-16], rax ;; char *wrenScript;
mov rdx, qword [rbp-16]
xlea rsi, szmod
mov esi, 0
mov rdi, [rel wrenVM]
xcall wrenInterpret
cmp rax, WREN_RESULT_SUCCESS
jg _main_noargs
jmp _main_exit ;; Let's gtfo of dodge.
_main_noargs:
xlea rdi, sznoargs
xcall printf
;; At this point we should free the mem but
;; the program ends so who gives a ....
_main_exit:
add rsp, 16
pop rbp
xor rdi, rdi
xcall exit
ret
;; We only care about the file name once, So.. No keepy keepy.
srcread:
push rbp
mov rbp, rsp
sub rsp, 32
xlea rsi,szread
xcall fopen
cmp rax, 0
jle _srcread_nofile
mov qword [rbp-8], rax ;; file handle.
mov edx, 2 ;; SEEK_END
mov esi, 0
mov rdi, qword [rbp-8]
xcall fseek
mov rdi, qword [rbp-8]
xcall ftell
mov qword [rbp-16], rax
mov rdi, qword [rbp-8]
xcall rewind
mov rax, qword [rbp-16]
add rax, 1
mov rdi, rax
xcall malloc
mov qword [rbp-24], rax
mov rcx, qword [rbp-8] ;; file handle
mov rdx, qword [rbp-16] ;; size
mov esi, 1
mov rdi, qword [rbp-24] ;; buffer
xcall fread
mov rdi, qword [rbp-8]
xcall fclose
mov rcx, qword [rbp-16]
mov rax, qword [rbp-24]
add rax, rcx
mov byte [rax], 0
jmp _srcread_exit
_srcread_nofile:
xlea rdi, sznofile
xcall printf
_srcread_exit:
mov rax, qword [rbp-24]
add rsp, 32
pop rbp
ret
;; Just prints whatever's given to it's one argument.
writefn:
push rbp
mov rbp, rsp
xlea rdi, pfmt
xcall printf
pop rbp
ret
errfn:
push rbp
mov rbp, rsp
xlea rdi, szothererr
xcall printf
pop rbp
ret
;; Still to lazy to do those if checks... -.-
;; I'll do it properly one day, I promise. :]
bindfn:
push rbp
mov rbp, rsp
xlea rax, C_strdup
pop rbp
ret

View file

@ -0,0 +1,5 @@
class C {
foreign static strdup(s)
}
var s = "Goodbye, World!"
System.print(C.strdup(s))

View file

@ -0,0 +1,13 @@
void free( void* ptr );
char * strdup( const char *str1 );
typedef struct WrenVM WrenVM;
const char* wrenGetSlotString(WrenVM* vm, int slot);
void wrenSetSlotString(WrenVM* vm, int slot, const char* text);
void C_strdup(WrenVM* vm) {
const char *s = wrenGetSlotString(vm, 1);
char *t = strdup(s);
wrenSetSlotString(vm, 0, t);
free(t);
}