From 537288b228a42153aa43bc0cf79cf13d419775a6 Mon Sep 17 00:00:00 2001 From: jbirddog <100367399+jbirddog@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:02:46 -0400 Subject: [PATCH] Support calling user words from other user words (#75) --- Makefile | 7 ++++++- blue.asm | 12 ++++++----- blue_test.asm | 29 ------------------------- debug.inc | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ dictionary.inc | 3 ++- kernel.inc | 40 ++++++++++++++++++++++++++++------- 6 files changed, 105 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 373c98a..fbc32a1 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,10 @@ run: dis: $(DISASM) $(WHAT) +disa: WHAT=a.out +disa: dis + @true + disb: ARGS=-b binary disb: dis @true @@ -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 diff --git a/blue.asm b/blue.asm index fc5a0a4..8feee41 100644 --- a/blue.asm +++ b/blue.asm @@ -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 diff --git a/blue_test.asm b/blue_test.asm index 31e753d..fd5b242 100644 --- a/blue_test.asm +++ b/blue_test.asm @@ -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: diff --git a/debug.inc b/debug.inc index 5e0b44d..14816aa 100644 --- a/debug.inc +++ b/debug.inc @@ -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 + diff --git a/dictionary.inc b/dictionary.inc index 69a8925..4c022fd 100644 --- a/dictionary.inc +++ b/dictionary.inc @@ -13,6 +13,7 @@ DE_HIDDEN = 1 DE_IMMEDIATE = 2 +DE_CORE = 4 FLAGS_OFFSET = 0 FLAGS_MASK = 0xff @@ -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 diff --git a/kernel.inc b/kernel.inc index d92c44f..b165904 100644 --- a/kernel.inc +++ b/kernel.inc @@ -63,7 +63,7 @@ kernel_deinit: ; expects ; - word in rax ; -_compile_word: +_compile_word_abs: push rax call flow_in @@ -85,6 +85,27 @@ _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 @@ -92,8 +113,8 @@ _compile_word: _interpret_word: push [_code_buffer.here] - call _compile_word - + call _compile_word_abs + mov al, 0xc3 call b_comma @@ -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