Skip to content

Commit

Permalink
Simple dictionary find routine (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Jun 23, 2024
1 parent 3fed5a2 commit fdca9c8
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 72 deletions.
9 changes: 9 additions & 0 deletions defs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ _code_buffer:
.base dq 1
.here dq 1
.entry dd 1

_dictionary:
.prev_word_offset = 8
.entry_code_offset = 16
.word_offset = 24
.length = 4096
.base dq 1
.here dq 1
.latest dq 1
105 changes: 105 additions & 0 deletions dictionary.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
;
; entry:
;
; 08 - 1 byte flags
; - 1 byte word len
; - 6 bytes reserved
; 08 - prev addr
; 08 - code addr
; ?? - word chars (padded to dq)
; ?? - stack effects
;

DE_HIDDEN = 1
DE_IMMEDIATE = 2

_core_words:
.b_comma:
db DE_IMMEDIATE
db 2
dw 0
dd 0
dq 0
dq _core_code.b_comma
dq 'b,'
dq 0 ; TODO: stack effects (( b al -- | rdi ))
.latest:
.d_comma:
db DE_IMMEDIATE
db 2
dw 0
dd 0
dq .b_comma
dq _core_code.d_comma
dq 'd,'
dq 0 ; TODO: stack effects (( d ax -- | rdi ))

dictionary_init:
mov esi, _dictionary.length
mov edx, PROT_RW
call mmap_buffer

mov rdi, _dictionary.base
stosq
stosq
mov [_dictionary.latest], _core_words.latest

ret

dictionary_deinit:
mov esi, _dictionary.length
mov rdi, [_dictionary.base]
mov eax, SYS_MUNMAP
syscall
ret

;
; expects
; - properly configured blue structure with non zero word len
;
dictionary_find:
mov rdi, [_dictionary.latest]

.find:
push rdi

test byte [edi], DE_HIDDEN
jnz .next

inc edi
mov rsi, _blue.word_len
mov ecx, [esi]
lodsb
cmp al, byte [edi]
jne .next

add edi, _dictionary.word_offset - 1
.check_word:
lodsb
cmp al, byte [edi]
jne .next

inc edi
dec ecx
js .check_word

pop rax
jmp .done
.next:
pop rdi
add edi, _dictionary.prev_word_offset
mov rdi, [rdi]

test rdi, rdi
jnz .find

xor eax, eax
.done:
ret
64 changes: 64 additions & 0 deletions dictionary_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
format elf64 executable 3

macro tc1 tib, tib_len, expected {
mov [_blue.tib], tib
mov [_blue.tib_len], tib_len
mov [_blue.tib_in], 0

call parser_next_word
call dictionary_find

inc [test_num]
cmp rax, expected
jne failure
}

segment readable writeable

include "defs.inc"

test_num db 1

segment readable executable

include "linux.inc"
include "code_buffer.inc"
include "dictionary.inc"
include "parser.inc"

entry $
mov [test_num], 0

call dictionary_init
inc [test_num]
mov rax, [_dictionary.base]
cmp rax, [_dictionary.here]
jne failure
inc [test_num]
mov rax, [_dictionary.latest]
cmp rax, _core_words.latest
jne failure

tc1 unknown, 4, 0
tc1 d_comma, 2, _core_words.d_comma
tc1 b_comma, 2, _core_words.b_comma
call dictionary_deinit

xor edi, edi
exit:
mov eax, 60
syscall

failure:
mov dil, [test_num]
jmp exit

segment readable

unknown db '!@#$'
b_comma db 'b,'
d_comma db 'd,'
49 changes: 0 additions & 49 deletions scratch/dictionary.inc

This file was deleted.

23 changes: 0 additions & 23 deletions scratch/dictionary_test.inc

This file was deleted.

1 change: 1 addition & 0 deletions test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
TESTS := \
code_buffer_test \
parser_test \
dictionary_test \
elf_test \
elf_test_hello_world

Expand Down

0 comments on commit fdca9c8

Please sign in to comment.