This commit is contained in:
Kimapr 2024-04-05 12:21:27 +05:00
parent 07c7f6df35
commit e1efc5acaf
5 changed files with 75 additions and 18 deletions

12
build
View file

@ -1,15 +1,17 @@
#!/bin/sh
mkdir -p target
./charjmpt.lua > target/charjmpt.s
gcc -c meow.s -o target/amogus.o || exit
gnu() { shift 1 && gcc "$@"; }
gnu() { shift 1 && clang "$@"; }
"$(command -v ~/stuff/zig/zig || echo gnu)" \
cc -O3 -shared -static -nostdlib -nodefaultlibs target/amogus.o -o target/amogus || exit
objdump -d target/amogus
strip -K entry target/amogus
objdump -t target/amogus
du -b target/amogus
cp target/amogus target/amogstrip
strip -K entry target/amogstrip
objdump -t target/amogstrip
du -b target/amogstrip
rm -rf target/cg/
mkdir -p target/cg
cp target/amogus target/cg/
cp target/amogstrip target/cg/amogus
cp amogus.h target/cg/

10
charjmpt.lua Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env lua
print("charjmpt:")
chars = {
['\0'] = "parse_exit",
}
local def = "parse_self"
for n=0,255 do
local c = string.char(n)
print("\t.4byte "..(chars[c] or "parse_self").." - charjmpt_prej")
end

8
ctest
View file

@ -3,5 +3,9 @@
export C_INCLUDE_PATH="$PWD:$C_INCLUDE_PATH"
export LD_LIBRARY_PATH="$PWD/target:$LD_LIBRARY_PATH"
export LIBRARY_PATH="$PWD/target:$LIBRARY_PATH"
gcc test.c -o target/cmogus -L"$LIBRARY_PATH" -l:amogus || exit
target/cmogus
gcc -O2 test.c -o target/cmogus -L"$LIBRARY_PATH" -l:amogus || exit
gdb -q \
-iex 'set confirm no' \
-ex 'layout asm' \
-ex 'start' \
target/cmogus

57
meow.s
View file

@ -3,7 +3,6 @@
.globl entry
.type entry, @function
_start:
#mov -4(%ebp),%eax
pop %rax
mov %rax,argc(%rip)
mov %rax,%rdi
@ -72,19 +71,59 @@ _exit:
syscall
ret
.include "target/charjmpt.s"
# rdi - where
# rsi - char
#parse_...:
parse_self:
xor %rax,%rax
ret
parse_exit:
mov $1,%rax
ret
# rdi - regex
# rsi - callback
# rdx - cb data
entry:
push %r12
sub $8,%r12
mov %rsp,%r12
push %rbp
mov %rsp,%rbp
sub $48,%rsp
mov %rsi,-8(%rbp)
mov %rdx,-16(%rbp)
mov %rdi,-24(%rbp)
entry_parse_begin:
test %rdi,%rdi
movzbl (%rdi),%ecx
lea charjmpt(%rip),%r11
movzx %cl,%r10
shl $2,%r10
add %r10,%r11
movsxd (%r11),%r11
charjmpt_prej:
lea charjmpt_prej(%rip),%r10
add %r10,%r11
mov %rdi,-32(%rbp)
call *%r11
test %rax,%rax
jnz entry_parse_end
mov %rdi,%rcx
mov -32(%rbp),%rdi
inc %rdi
jmp entry_parse_begin
entry_parse_end:
mov %r12,%rsp
add $8,%r12
pop %r12
mov %rdi,%rsi
mov -24(%rbp),%rdi
sub %rdi,%rsi
inc %rsi
mov -16(%rbp),%rdx
mov -8(%rbp),%r9
entry_pre_call:
call *%r9
mov %rbp,%rsp
pop %rbp
ret
.bss

6
test.c
View file

@ -6,12 +6,14 @@
void callback(char* str, int size, void* data) {
char** out = (char**)data;
*out = (char*)malloc(size);
memcpy(str, *out, size);
if(str!=NULL)
memcpy(*out, str, size);
else abort();
}
int main() {
char* match;
if(!entry("meow.*|m(r(r(p..*)))",callback,&match))
if(!entry("me\\|\\|ow.*|m(r(r(p..*)))",callback,&match))
return 1;
printf("%s",match);
}