Make it JIT lmao
This commit is contained in:
324
jitfuck.asm
324
jitfuck.asm
@@ -1,24 +1,43 @@
|
|||||||
format ELF64 executable
|
format ELF64 executable
|
||||||
|
|
||||||
RIGHT = 3Eh
|
RIGHT = 0x3E
|
||||||
LEFT = 3Ch
|
LEFT = 0x3C
|
||||||
PLUS = 2Bh
|
PLUS = 0x2B
|
||||||
MINUS = 2Dh
|
MINUS = 0x2D
|
||||||
DOT = 2Eh
|
DOT = 0x2E
|
||||||
COMMA = 2Ch
|
COMMA = 0x2C
|
||||||
LBRAC = 5Bh
|
LBRAC = 0x5B
|
||||||
RBRAC = 5Dh
|
RBRAC = 0x5D
|
||||||
|
|
||||||
CHUNK_MAX = 20h
|
CHUNK_MAX = 0x40
|
||||||
CHUNK_SIZE = (CHUNK_MAX * 2)
|
CHUNK_SIZE = (CHUNK_MAX * 2)
|
||||||
CHUNK_MASK = (CHUNK_SIZE - 1)
|
CHUNK_MASK = (CHUNK_SIZE - 1)
|
||||||
|
|
||||||
MAX_CHUNKS = 20h
|
MAX_CHUNKS = 0x20
|
||||||
TRANSLATION_MAX = 40h
|
|
||||||
|
|
||||||
MEM = 100h
|
MEM = 0x100
|
||||||
|
|
||||||
entry $
|
entry $
|
||||||
|
mov rdi,0
|
||||||
|
mov rsi,(CHUNK_SIZE * MAX_CHUNKS)
|
||||||
|
mov rdx,0x07 ; PROT_READ | PROT_WRITE | PROT_EXEC
|
||||||
|
mov r10,0x22 ; MAP_PRIVATE | MAP_ANONYMOUS
|
||||||
|
mov r8,-1
|
||||||
|
mov r9,0
|
||||||
|
mov rax,9 ; mmap
|
||||||
|
syscall
|
||||||
|
cmp rax,-1
|
||||||
|
je fail
|
||||||
|
mov [translations],rax
|
||||||
|
xor rbx,rbx
|
||||||
|
.kekw:
|
||||||
|
cmp rbx,(CHUNK_SIZE * MAX_CHUNKS)
|
||||||
|
jge .kekwdone
|
||||||
|
mov byte [rax+rbx],0
|
||||||
|
inc rbx
|
||||||
|
jmp .kekw
|
||||||
|
.kekwdone:
|
||||||
|
|
||||||
xor r12,r12 ; Initialize chunk start
|
xor r12,r12 ; Initialize chunk start
|
||||||
.next_chunk:
|
.next_chunk:
|
||||||
xor r13,r13 ; Initialize chunk offset
|
xor r13,r13 ; Initialize chunk offset
|
||||||
@@ -26,7 +45,7 @@ entry $
|
|||||||
call key
|
call key
|
||||||
.next_run:
|
.next_run:
|
||||||
cmp rax,0
|
cmp rax,0
|
||||||
je interpret ; EOF -> finished loading chunks
|
je execute ; EOF -> finished loading chunks
|
||||||
|
|
||||||
;; Dispatch on command
|
;; Dispatch on command
|
||||||
cmp rax,RIGHT
|
cmp rax,RIGHT
|
||||||
@@ -81,7 +100,7 @@ entry $
|
|||||||
pop rax ; Pop address of lbrac's dest field
|
pop rax ; Pop address of lbrac's dest field
|
||||||
mov rbx,r12
|
mov rbx,r12
|
||||||
add rbx,CHUNK_SIZE
|
add rbx,CHUNK_SIZE
|
||||||
mov word [rax],bx ; Write start of next chunk to lbrac's dest field
|
mov word [rax],bx ; Write start of next chunk to lbrac's dest
|
||||||
mov rax,r12
|
mov rax,r12
|
||||||
add rax,r13
|
add rax,r13
|
||||||
mov byte [chunks+rax],RBRAC ; Write command
|
mov byte [chunks+rax],RBRAC ; Write command
|
||||||
@@ -91,85 +110,217 @@ entry $
|
|||||||
jmp .end_chunk
|
jmp .end_chunk
|
||||||
|
|
||||||
.end_chunk:
|
.end_chunk:
|
||||||
mov word [chunks+rax+4],0 ; Write chunk terminator
|
|
||||||
add r12,CHUNK_SIZE ; Bump chunk start to next chunk
|
add r12,CHUNK_SIZE ; Bump chunk start to next chunk
|
||||||
jmp .next_chunk
|
jmp .next_chunk
|
||||||
|
|
||||||
interpret:
|
execute:
|
||||||
|
mov rax,r12
|
||||||
|
add rax,r13
|
||||||
|
mov word [chunks+rax+4],0 ; Terminate last chunk
|
||||||
|
add r12,CHUNK_SIZE
|
||||||
|
|
||||||
|
xor rax,rax
|
||||||
|
mov rbx,[translations]
|
||||||
|
.init_translations:
|
||||||
|
cmp rax,r12
|
||||||
|
je .begin
|
||||||
|
mov word [rbx+rax],0x0B0F ; Write UD2 to start
|
||||||
|
add rax,CHUNK_SIZE
|
||||||
|
jmp .init_translations
|
||||||
|
.begin:
|
||||||
|
mov r15,r12 ; Save end point
|
||||||
xor r12,r12 ; Initialize code offset
|
xor r12,r12 ; Initialize code offset
|
||||||
mov r13,mem ; Initialize head
|
mov r13,mem ; Initialize head
|
||||||
.opcode_dispatch:
|
|
||||||
mov word ax,[chunks+r12]
|
|
||||||
movzx ebx,ah
|
|
||||||
cmp al,RIGHT
|
|
||||||
je .right
|
|
||||||
cmp al,LEFT
|
|
||||||
je .left
|
|
||||||
cmp al,PLUS
|
|
||||||
je .plus
|
|
||||||
cmp al,MINUS
|
|
||||||
je .minus
|
|
||||||
cmp al,DOT
|
|
||||||
je .dot
|
|
||||||
cmp al,COMMA
|
|
||||||
je .comma
|
|
||||||
cmp al,LBRAC
|
|
||||||
je .lbrac
|
|
||||||
cmp al,RBRAC
|
|
||||||
je .rbrac
|
|
||||||
jmp exit
|
|
||||||
|
|
||||||
.right:
|
.check:
|
||||||
add r13,rbx
|
cmp r12,r15
|
||||||
jmp .next
|
je dump
|
||||||
|
mov rbx,[translations]
|
||||||
|
mov word ax,[rbx+r12]
|
||||||
|
cmp ax,0x0B0F
|
||||||
|
je translate
|
||||||
|
.run:
|
||||||
|
;jmp .next_chunk
|
||||||
|
|
||||||
.left:
|
mov r14,.check
|
||||||
sub r13,rbx
|
mov rbx,[translations]
|
||||||
jmp .next
|
add rbx,r12
|
||||||
|
jmp rbx
|
||||||
.plus:
|
|
||||||
add byte [r13],bl
|
|
||||||
jmp .next
|
|
||||||
|
|
||||||
.minus:
|
|
||||||
sub byte [r13],bl
|
|
||||||
jmp .next
|
|
||||||
|
|
||||||
.dot:
|
|
||||||
mov r14,rbx
|
|
||||||
.dot_loop:
|
|
||||||
cmp r14,0
|
|
||||||
je .next
|
|
||||||
call emit
|
|
||||||
dec r14
|
|
||||||
jmp .dot_loop
|
|
||||||
|
|
||||||
.comma: ; TODO
|
|
||||||
int3
|
|
||||||
jmp .next
|
|
||||||
|
|
||||||
.lbrac:
|
|
||||||
cmp byte [r13],0
|
|
||||||
jne .next_chunk
|
|
||||||
mov word r12w,[chunks+r12+2]
|
|
||||||
jmp .opcode_dispatch
|
|
||||||
|
|
||||||
.rbrac:
|
|
||||||
cmp byte [r13],0
|
|
||||||
je .next_chunk
|
|
||||||
mov word r12w,[chunks+r12+2]
|
|
||||||
jmp .opcode_dispatch
|
|
||||||
|
|
||||||
.next:
|
|
||||||
add r12,2
|
|
||||||
jmp .opcode_dispatch
|
|
||||||
|
|
||||||
.next_chunk:
|
.next_chunk:
|
||||||
and r12,(not CHUNK_MASK)
|
and r12,(not CHUNK_MASK)
|
||||||
add r12,CHUNK_SIZE
|
add r12,CHUNK_SIZE
|
||||||
jmp .opcode_dispatch
|
jmp .check
|
||||||
|
|
||||||
exit:
|
translate:
|
||||||
|
mov r14,[translations]
|
||||||
|
add r14,r12 ; Initialize translation write offset
|
||||||
|
.opcode:
|
||||||
|
mov word ax,[chunks+r12]
|
||||||
|
movzx ebx,ah
|
||||||
|
cmp al,RIGHT
|
||||||
|
je right
|
||||||
|
cmp al,LEFT
|
||||||
|
je left
|
||||||
|
cmp al,PLUS
|
||||||
|
je plus
|
||||||
|
cmp al,MINUS
|
||||||
|
je minus
|
||||||
|
cmp al,DOT
|
||||||
|
je dot
|
||||||
|
cmp al,COMMA
|
||||||
|
je comma
|
||||||
|
cmp al,LBRAC
|
||||||
|
je lbrac
|
||||||
|
cmp al,RBRAC
|
||||||
|
je rbrac
|
||||||
|
cmp al,0
|
||||||
|
je term
|
||||||
|
jmp fail
|
||||||
|
|
||||||
|
.next:
|
||||||
|
add r12,2
|
||||||
|
jmp .opcode
|
||||||
|
|
||||||
|
write:
|
||||||
|
cmp rdi,rsi
|
||||||
|
je .done
|
||||||
|
mov byte dl,[rdi]
|
||||||
|
mov byte [r14],dl
|
||||||
|
inc rdi
|
||||||
|
inc r14
|
||||||
|
jmp write
|
||||||
|
.done:
|
||||||
|
ret
|
||||||
|
|
||||||
|
right:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov byte [r14-4],bl
|
||||||
|
jmp translate.next
|
||||||
|
.translation_start:
|
||||||
|
add r13,0xFF
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
left:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov byte [r14-4],bl
|
||||||
|
jmp translate.next
|
||||||
|
.translation_start:
|
||||||
|
sub r13,0xFF
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
plus:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov byte [r14-1],bl
|
||||||
|
jmp translate.next
|
||||||
|
.translation_start:
|
||||||
|
add byte [r13],0xFF
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
minus:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov byte [r14-1],bl
|
||||||
|
jmp translate.next
|
||||||
|
.translation_start:
|
||||||
|
sub byte [r13],0xFF
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
dot:
|
||||||
|
mov rsi,.translation_end
|
||||||
|
.loop:
|
||||||
|
cmp rbx,0
|
||||||
|
je translate.next
|
||||||
|
mov rdi,.translation_start
|
||||||
|
call write
|
||||||
|
dec rbx
|
||||||
|
jmp .loop
|
||||||
|
.rt_impl:
|
||||||
|
mov rdi,1
|
||||||
|
mov rsi,r13
|
||||||
|
mov rdx,1
|
||||||
|
mov rax,1
|
||||||
|
syscall
|
||||||
|
ret
|
||||||
|
.translation_start:
|
||||||
|
mov rax,.rt_impl
|
||||||
|
call rax
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
comma: ; TODO
|
||||||
|
int3
|
||||||
|
jmp translate.next
|
||||||
|
|
||||||
|
lbrac:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov word ax,[chunks+r12+2]
|
||||||
|
mov byte [r14-7],al
|
||||||
|
shr rax,8
|
||||||
|
mov byte [r14-6],al
|
||||||
|
and r12,(not CHUNK_MASK)
|
||||||
|
jmp execute.run
|
||||||
|
.translation_start:
|
||||||
|
cmp byte [r13],0
|
||||||
|
je .jump
|
||||||
|
add r12,(CHUNK_SIZE)
|
||||||
|
jmp r14
|
||||||
|
.jump:
|
||||||
|
mov r12,0xFFFF
|
||||||
|
jmp r14
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
rbrac:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
mov word ax,[chunks+r12+2]
|
||||||
|
mov byte [r14-7],al
|
||||||
|
shr rax,8
|
||||||
|
mov byte [r14-6],al
|
||||||
|
and r12,(not CHUNK_MASK)
|
||||||
|
jmp execute.run
|
||||||
|
.translation_start:
|
||||||
|
cmp byte [r13],0
|
||||||
|
jne .jump
|
||||||
|
add r12,(CHUNK_SIZE)
|
||||||
|
jmp r14
|
||||||
|
.jump:
|
||||||
|
mov r12,0xFFFF
|
||||||
|
jmp r14
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
term:
|
||||||
|
mov rdi,.translation_start
|
||||||
|
mov rsi,.translation_end
|
||||||
|
call write
|
||||||
|
and r12,(not CHUNK_MASK)
|
||||||
|
jmp execute.run
|
||||||
|
.translation_start:
|
||||||
|
mov rdi,0
|
||||||
|
mov rax,60
|
||||||
|
syscall
|
||||||
|
.translation_end:
|
||||||
|
|
||||||
|
fail:
|
||||||
|
mov rdi,1
|
||||||
|
mov rax,60
|
||||||
|
syscall
|
||||||
|
|
||||||
|
dump:
|
||||||
|
mov rdi,1
|
||||||
|
mov rsi,[translations]
|
||||||
|
mov rdx,(CHUNK_SIZE * MAX_CHUNKS)
|
||||||
|
mov rax,1
|
||||||
|
syscall
|
||||||
mov rdi,0
|
mov rdi,0
|
||||||
mov rax,60
|
mov rax,60
|
||||||
syscall
|
syscall
|
||||||
@@ -189,14 +340,7 @@ key:
|
|||||||
ret
|
ret
|
||||||
.ch: rb 1
|
.ch: rb 1
|
||||||
|
|
||||||
emit:
|
|
||||||
mov rdi,1
|
|
||||||
mov rsi,r13
|
|
||||||
mov rdx,1
|
|
||||||
mov rax,1
|
|
||||||
syscall
|
|
||||||
ret
|
|
||||||
|
|
||||||
chunks: rw (CHUNK_MAX * MAX_CHUNKS)
|
chunks: rw (CHUNK_MAX * MAX_CHUNKS)
|
||||||
translations: rb (TRANSLATION_MAX * MAX_CHUNKS)
|
|
||||||
mem: db MEM dup 0
|
mem: db MEM dup 0
|
||||||
|
|
||||||
|
translations: rq 1
|
||||||
|
|||||||
Reference in New Issue
Block a user