Skip to content

Commit

Permalink
Support calling user words from other user words (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Jul 6, 2024
1 parent 0c1d4c7 commit 537288b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 43 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ run:
dis:
$(DISASM) $(WHAT)

disa: WHAT=a.out
disa: dis
@true

disb: ARGS=-b binary
disb: dis
@true
Expand All @@ -49,4 +53,5 @@ clean:

.PHONY:
dev-env dev-start dev-stop \
compile run dis disb discb clean
compile run clean \
dis disa disb discb
12 changes: 7 additions & 5 deletions blue.asm
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ segment readable
blue_bye:
db '16 base '
db ''
db ': bye '
db ' 31 b, FF b, '
db ' B8 b, 3C d, '
db ' 0F b, 05 b, '
db '; immediate '
db ': xor-edi 31 b, FF b, ; '
db ': mov-eax-60 B8 b, 3C d, ; '
db ': syscall 0F b, 05 b, ; '
db ''
db ': ok xor-edi ; '
db ': exit mov-eax-60 syscall ; '
db ': bye ok exit ; immediate '
db ''
db ': _start bye ; entry '
.length = $ - blue_bye
Expand Down
29 changes: 0 additions & 29 deletions blue_test.asm
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,8 @@ failure:
mov eax, 60
syscall

print_char:
mov edx, 1

print:
mov edi, STDOUT_FILENO
mov eax, SYS_WRITE
syscall
ret

here:
push rax
push rcx
push rdi
push rdx
push rsi
mov esi, H
call print_char

pop rsi
pop rdx
pop rdi
pop rcx
pop rax

ret
newline db 10
dot db '.'
X db 'X'
H db 'H'
ts_ok db ' ok', 10

header:
Expand Down
57 changes: 57 additions & 0 deletions debug.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,60 @@ dump_code_buffer:
_code_buffer_dump_file:
db "code_buffer_dump.out"
db 0x00

H db 'H'
newline db 10

print_char:
mov edx, 1

print:
mov edi, STDOUT_FILENO
mov eax, SYS_WRITE
syscall
ret

print_word:
push rax
push rcx
push rdi
push rdx
push rsi

xor edx, edx
mov dl, [_blue.word_len]
mov esi, _blue.word
call print

mov esi, newline
call print_char

pop rsi
pop rdx
pop rdi
pop rcx
pop rax

ret
here:
push rax
push rcx
push rdi
push rdx
push rsi
mov esi, H
call print_char

mov esi, newline
call print_char

pop rsi
pop rdx
pop rdi
pop rcx
pop rax

ret

3 changes: 2 additions & 1 deletion dictionary.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

DE_HIDDEN = 1
DE_IMMEDIATE = 2
DE_CORE = 4

FLAGS_OFFSET = 0
FLAGS_MASK = 0xff
Expand All @@ -25,7 +26,7 @@ IN_EFFECTS_LEN_MASK = 0x0000ff

macro __w word, word_len, flags, code, ise_len, ose_len {
.##code:
db flags, word_len, ise_len, ose_len
db flags or DE_CORE, word_len, ise_len, ose_len
dd 0
dq _core_words.prev_word
dq code
Expand Down
40 changes: 33 additions & 7 deletions kernel.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ kernel_deinit:
; expects
; - word in rax
;
_compile_word:
_compile_word_abs:
push rax
call flow_in

Expand All @@ -85,15 +85,36 @@ _compile_word:

ret

;
; expects
; - word in rax
;
_compile_word_rel:
push rax
call flow_in

mov al, 0xe8
call b_comma

pop rax
add rax, _dictionary.code_offset
mov rax, [rax]
sub rax, [_code_buffer.here]
sub rax, 4

call d_comma

ret

;
; expects
; - word in rax
;
_interpret_word:
push [_code_buffer.here]

call _compile_word

call _compile_word_abs
mov al, 0xc3
call b_comma

Expand All @@ -114,12 +135,17 @@ _handle_word:
mov rsi, [rax]
test sil, DE_IMMEDIATE
jnz .interpret
test sil, DE_CORE
jnz .compile_core

call _compile_word
jmp .done
call _compile_word_rel
ret
.compile_core:
call _compile_word_abs
ret

.interpret:
call _interpret_word

.done:
ret

0 comments on commit 537288b

Please sign in to comment.