forth-riscv

My forth
git clone git://git.electrosoup.com/forth-riscv
Log | Files | Refs

commit c66ecfa37d879c0570763964d6ea54b0b0b99f6f
parent 73cf5d43d5d4dd6833bb89e9205b4391c19ee566
Author: Christian Ermann <christianermann@gmail.com>
Date:   Wed, 30 Oct 2024 14:38:35 -0700

Convert tabs to spaces

Diffstat:
Mforth.s | 818++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 409 insertions(+), 409 deletions(-)

diff --git a/forth.s b/forth.s @@ -28,625 +28,625 @@ .equ flag_immediate, 0x40 .macro next - load_cell w, 0(ip) - load_cell x, 0(w) - addi ip, ip, cell - jr x + load_cell w, 0(ip) + load_cell x, 0(w) + addi ip, ip, cell + jr x .endm .macro push_addr addr - la w, version_string - push w + la w, version_string + push w .endm .macro push_imm imm - li w, \imm - push w + li w, \imm + push w .endm .macro push reg - store_cell \reg, -cell(psp) - addi psp, psp, -cell + store_cell \reg, -cell(psp) + addi psp, psp, -cell .endm .macro pop reg - load_cell \reg, 0(psp) - addi psp, psp, cell + load_cell \reg, 0(psp) + addi psp, psp, cell .endm .macro push_ret reg - store_cell \reg, -cell(rsp) - addi rsp, rsp, -cell + store_cell \reg, -cell(rsp) + addi rsp, rsp, -cell .endm .macro pop_ret reg - load_cell \reg, 0(rsp) - addi rsp, rsp, cell + load_cell \reg, 0(rsp) + addi rsp, rsp, cell .endm .macro defcode name, name_length, flags, hash, label, last - .section ".rodata" - .balign cell - .globl name_\label + .section ".rodata" + .balign cell + .globl name_\label name_\label: - .int name_\last # 1. Link to the previously defined word. - .int \hash - .int meta_\label - .byte \flags # 3. Set the flags. + .int name_\last # 1. Link to the previously defined word. + .int \hash + .int meta_\label + .byte \flags # 3. Set the flags. - .balign cell # 6. Add any padding we may need. - .globl \label + .balign cell # 6. Add any padding we may need. + .globl \label \label: - .int code_\label # 7. Set the codeword. + .int code_\label # 7. Set the codeword. - .section ".data.meta" - .global meta_\label + .section ".data.meta" + .global meta_\label meta_\label: - .byte \name_length - .ascii "\name" + .byte \name_length + .ascii "\name" - .section ".text" - .globl code_\label + .section ".text" + .globl code_\label code_\label: # 8. This is where our assembly code will go. .endm .macro defword name, name_length, flags, hash, label, last - .section ".rodata" - .balign cell - .globl name_\label + .section ".rodata" + .balign cell + .globl name_\label name_\label: - .int name_\last - .int \hash - .int meta_\label - .byte \flags + .int name_\last + .int \hash + .int meta_\label + .byte \flags - .section ".data.meta" - .global meta_\label + .section ".data.meta" + .global meta_\label meta_\label: - .byte \name_length - .ascii "\name" + .byte \name_length + .ascii "\name" - .section ".rodata" - .balign cell - .globl \label + .section ".rodata" + .balign cell + .globl \label \label: - .int docol + .int docol .endm docol: - push_ret ip - addi ip, w, cell - NEXT + push_ret ip + addi ip, w, cell + NEXT .equ name_null, 0 defcode "exit", 4, 0, 0xCDED1A85, exit, null - pop_ret ip - next + pop_ret ip + next defcode "type", 4, 0, 0x5127F14D, type, exit - pop a1 # length - pop a0 # address - jal uart_put_string - next + pop a1 # length + pop a0 # address + jal uart_put_string + next defcode "emit", 4, 0, 0x2D88474A, emit, type - pop a0 - jal uart_put_char - next + pop a0 + jal uart_put_char + next defcode "key", 3, 0, 0x6815C86C, key, emit - jal uart_get_char - push a0 - next + jal uart_get_char + push a0 + next defcode "accept", 6, 0, 0x08247E29, accept, key - pop w # max len - pop x # address - jal accept_impl - push w # recv'd len - next + pop w # max len + pop x # address + jal accept_impl + push w # recv'd len + next _input_get_char: .int uart_get_char defcode "accept-uart", 11, 0, 0xEA73CDE0, accept_uart, accept - la w, _input_get_char - la x, uart_get_char - store_cell x, 0(w) - next + la w, _input_get_char + la x, uart_get_char + store_cell x, 0(w) + next defcode "accept-file", 11, 0, 0xD269E17A, accept_file, accept_uart - la w, _input_get_char - la x, file_get_char - store_cell x, 0(w) - next + la w, _input_get_char + la x, file_get_char + store_cell x, 0(w) + next accept_impl: - push_ret ra - mv s4, w - mv s5, w - beqz s5, 2f - li s6, 0x0A # '\n' - li s7, 0x0D # '\r' + push_ret ra + mv s4, w + mv s5, w + beqz s5, 2f + li s6, 0x0A # '\n' + li s7, 0x0D # '\r' 1: - la t0, _input_get_char - load_cell t0, 0(t0) - push x - jalr t0 - pop x - beq w, s6, 2f - beq w, s7, 2f - sb w, 0(x) - addi x, x, 1 - addi s5, s5, -1 - bnez s5, 1b + la t0, _input_get_char + load_cell t0, 0(t0) + push x + jalr t0 + pop x + beq w, s6, 2f + beq w, s7, 2f + sb w, 0(x) + addi x, x, 1 + addi s5, s5, -1 + bnez s5, 1b 2: - sub w, s4, s5 - pop_ret ra - ret + sub w, s4, s5 + pop_ret ra + ret defword "refill-buffer", 13, 0, 0x4B9E9FEE, refill_buffer, accept_file - # ( addr max-len -- recv'd-len ) - .int two_dup, accept, dup, q_branch, _refill_buffer_failed - .int source_len, store - .int drop, source_addr, store - .int lit, 0, source_offset, store - .int lit, -1 - .int exit + # ( addr max-len -- recv'd-len ) + .int two_dup, accept, dup, q_branch, _refill_buffer_failed + .int source_len, store + .int drop, source_addr, store + .int lit, 0, source_offset, store + .int lit, -1 + .int exit _refill_buffer_failed: - .int swap, drop, swap, drop, exit + .int swap, drop, swap, drop, exit defword "refill", 6, 0, 0x238BAA91, refill, refill_buffer - .int source_id, fetch, q_branch, _refill_tib + .int source_id, fetch, q_branch, _refill_tib _refill_fib: - .int fib, refill_buffer - .int exit + .int fib, refill_buffer + .int exit _refill_tib: - .int tib, refill_buffer - .int exit + .int tib, refill_buffer + .int exit defcode "source", 6, 0, 0x238BAA91, source, refill - la w, _source - load_cell w, 0(w) - la x, _source_len - load_cell x, 0(x) - push w - push x - next + la w, _source + load_cell w, 0(w) + la x, _source_len + load_cell x, 0(x) + push w + push x + next defcode "source-addr", 11, 0, 0x997F9EE8, source_addr, source - la w, _source - push w - next + la w, _source + push w + next defcode "source-len", 10, 0, 0x1B513E8E, source_len, source_addr - la w, _source_len - push w - next + la w, _source_len + push w + next defcode "source-offset", 13, 0, 0x7F8C1674, source_offset, source_len - la w, _input_offset - push w - next + la w, _input_offset + push w + next defcode "source-id", 9, 0, 0x965ED1E2, source_id, source_offset - la w, _source_id - push w - next + la w, _source_id + push w + next defcode "tib", 3, 0, 0xC90B0194, tib, source_id - # ( -- addr len ) - la w, _tib - push w - la w, _tib_len - load_cell w, 0(w) - push w - next + # ( -- addr len ) + la w, _tib + push w + la w, _tib_len + load_cell w, 0(w) + push w + next defcode "fib", 3, 0, 0xBCE49236, fib, tib - # ( -- addr len ) - la w, _fib - push w - la w, _fib_len - load_cell w, 0(w) - push w - next + # ( -- addr len ) + la w, _fib + push w + la w, _fib_len + load_cell w, 0(w) + push w + next defcode "parse-word", 10, 0, 0xB218226F, parse_word, fib - la w, _source - load_cell w, 0(w) # buff addr - la x, _source_len - load_cell x, 0(x) # buff len - # apply input offset - la y, _input_offset - load_cell y, 0(y) - add w, w, y - sub x, x, y - - li s3, 0x20 # space - mv s4, w + la w, _source + load_cell w, 0(w) # buff addr + la x, _source_len + load_cell x, 0(x) # buff len + # apply input offset + la y, _input_offset + load_cell y, 0(y) + add w, w, y + sub x, x, y + + li s3, 0x20 # space + mv s4, w 1: # find start of word - beqz x, 3f # out of chars? -> stop! - lb t0, 0(w) # next char! - addi w, w, 1 # inc. addr - addi y, y, 1 # inc. offset - addi x, x, -1 # dec. len - beq t0, s3, 1b # space? -> skip! - - addi s4, w, -1 # addr of word + beqz x, 3f # out of chars? -> stop! + lb t0, 0(w) # next char! + addi w, w, 1 # inc. addr + addi y, y, 1 # inc. offset + addi x, x, -1 # dec. len + beq t0, s3, 1b # space? -> skip! + + addi s4, w, -1 # addr of word 2: # find end of word - beqz x, 3f - lb t0, 0(w) # next char! - addi y, y, 1 # inc. offset - beq t0, s3, 3f # space? -> stop! - addi w, w, 1 # inc. addr - addi x, x, -1 # dec. len - j 2b + beqz x, 3f + lb t0, 0(w) # next char! + addi y, y, 1 # inc. offset + beq t0, s3, 3f # space? -> stop! + addi w, w, 1 # inc. addr + addi x, x, -1 # dec. len + j 2b 3: # done - la t0, _input_offset - store_cell y, 0(t0) - sub x, w, s4 - push s4 # word addr - push x # word len - next + la t0, _input_offset + store_cell y, 0(t0) + sub x, w, s4 + push s4 # word addr + push x # word len + next defcode "word>hash", 9, 0, 0x50E0A245, word_to_hash, parse_word - pop w # word address - load_cell w, hash_offset(w) - push w # word hash - next + pop w # word address + load_cell w, hash_offset(w) + push w # word hash + next defword "find", 4, 0, 0xBDF0855A, find, word_to_hash - .int hash - .int latest, fetch + .int hash + .int latest, fetch _find_loop: - .int dup, zero_equal, q_branch, _find_check_hash - .int swap, drop, exit + .int dup, zero_equal, q_branch, _find_check_hash + .int swap, drop, exit _find_check_hash: - .int two_dup - .int word_to_hash, equal, q_branch, _find_next_word - .int swap, drop, exit + .int two_dup + .int word_to_hash, equal, q_branch, _find_next_word + .int swap, drop, exit _find_next_word: - .int fetch - .int branch, _find_loop + .int fetch + .int branch, _find_loop defcode ">cfa", 4, 0, 0x8CAC3233, to_cfa, find - pop w - addi w, w, code_offset - push w - next + pop w + addi w, w, code_offset + push w + next defcode "execute", 7, 0, 0xA01E3D98, execute, to_cfa - pop w - load_cell x, 0(w) - jr x - # 'next' should be called by the executed word + pop w + load_cell x, 0(w) + jr x + # 'next' should be called by the executed word defword "interpret", 9, 0, 0x1F98C57A, interpret, execute _interpret_start: - .int parse_word, dup, q_branch, _interpret_parse_area_empty - .int find, dup, q_branch, _interpret_word_not_found - .int to_cfa - .int compiling_q, q_branch, _interpret_execute_word - .int dup - .int immediate_q, zero_equal, q_branch, _interpret_execute_word + .int parse_word, dup, q_branch, _interpret_parse_area_empty + .int find, dup, q_branch, _interpret_word_not_found + .int to_cfa + .int compiling_q, q_branch, _interpret_execute_word + .int dup + .int immediate_q, zero_equal, q_branch, _interpret_execute_word _interpret_compile_word: - .int comma, branch, _interpret_start + .int comma, branch, _interpret_start _interpret_execute_word: - .int execute, branch, _interpret_start + .int execute, branch, _interpret_start _interpret_parse_area_empty: - .int drop, drop, exit + .int drop, drop, exit _interpret_word_not_found: - .int drop, branch, _interpret_start + .int drop, branch, _interpret_start defword "evaluate", 8, 0, 0xACE4360A, evaluate, interpret - .int lit, 1, source_id, store - .int accept_file + .int lit, 1, source_id, store + .int accept_file _evaluate_loop: - .int refill, q_branch, _evaluate_done - .int interpret - .int branch, _evaluate_loop + .int refill, q_branch, _evaluate_done + .int interpret + .int branch, _evaluate_loop _evaluate_done: - .int lit, 0, source_id, store - .int accept_uart - .int exit + .int lit, 0, source_id, store + .int accept_uart + .int exit defcode "branch", 6, 0, 0xB6873945, branch, evaluate - load_cell w, 0(ip) - mv ip, w - next + load_cell w, 0(ip) + mv ip, w + next defcode "?branch", 7, 0, 0x6AF3C1DE, q_branch, branch - pop w - bnez w, _branch_done - load_cell w, 0(ip) - mv ip, w - next + pop w + bnez w, _branch_done + load_cell w, 0(ip) + mv ip, w + next _branch_done: - addi ip, ip, cell - next + addi ip, ip, cell + next defword "quit", 4, 0, 0x47878736, quit, q_branch _quit_top: - .int refill, drop # TODO: What should happen when 'refill' fails? - .int interpret - .int branch, _quit_top + .int refill, drop # TODO: What should happen when 'refill' fails? + .int interpret + .int branch, _quit_top # ----------------------------------------------------------------------------- # memory access # ----------------------------------------------------------------------------- defcode "!", 1, 0, 0x240C8DEC, store, quit - pop w # address - pop x # value - store_cell x, 0(w) - next + pop w # address + pop x # value + store_cell x, 0(w) + next defcode "@", 1, 0, 0xC50BF85F, fetch, store - pop w # address - load_cell x, 0(w) - push x - next + pop w # address + load_cell x, 0(w) + push x + next defcode "c!", 2, 0, 0x9829F909, char_store, fetch - pop w - pop x - sb x, 0(w) - next + pop w + pop x + sb x, 0(w) + next defcode "c@", 2, 0, 0x37296056, char_fetch, char_store - pop w - lb x, 0(w) - push x - next + pop w + lb x, 0(w) + push x + next defcode "+!", 2, 0, 0x08DC01D1, add_store, char_fetch - pop w # address - pop x # value to add - load_cell t0, 0(w) - add t0, t0, x - store_cell t0, 0(w) - next + pop w # address + pop x # value to add + load_cell t0, 0(w) + add t0, t0, x + store_cell t0, 0(w) + next defcode "-!", 2, 0, 0x24CD235B, sub_store, add_store - pop w # address - pop x # value to add - load_cell t0, 0(w) - sub t0, t0, x - store_cell t0, 0(w) - next + pop w # address + pop x # value to add + load_cell t0, 0(w) + sub t0, t0, x + store_cell t0, 0(w) + next # ----------------------------------------------------------------------------- # dictionary management # ----------------------------------------------------------------------------- defcode "dp", 2, 0, 0x491CB0B9, dp, sub_store - la w, _here - push w - next + la w, _here + push w + next defcode "here", 4, 0, 0x213B65CB, here, dp - la w, _here - load_cell x, 0(w) - push x - next + la w, _here + load_cell x, 0(w) + push x + next defcode "latest", 6, 0, 0x41704246, latest, here - la w, _latest - push w - next + la w, _latest + push w + next defcode "lit", 3, 0, 0x404CD5B6, lit, latest - load_cell w, 0(ip) - addi ip, ip, cell - push w - next + load_cell w, 0(ip) + addi ip, ip, cell + push w + next defword "allot", 5, 0, 0xADB1A69F, allot, lit - .int dp, add_store - .int exit + .int dp, add_store + .int exit defword ",", 1, 0, 0x290C95CB, comma, allot - .int here, store # store value - .int lit, cell, allot # increment 'here' - .int exit + .int here, store # store value + .int lit, cell, allot # increment 'here' + .int exit defword "c,", 2, 0, 0xA32A0A5A, char_comma, comma - .int here, char_store # store value - .int lit, 1, allot # increment 'here' - .int exit + .int here, char_store # store value + .int lit, 1, allot # increment 'here' + .int exit defcode "align", 5, 0, 0xC73174DF, align, char_comma - la w, _here - load_cell x, 0(w) - addi x, x, 3 - andi x, x, 0xFFFFFFFC - store_cell x, 0(w) - next + la w, _here + load_cell x, 0(w) + addi x, x, 3 + andi x, x, 0xFFFFFFFC + store_cell x, 0(w) + next # ----------------------------------------------------------------------------- # stack manipulation # ----------------------------------------------------------------------------- defcode "dup", 3, 0, 0xD330F226, dup, align - load_cell w, 0(psp) - push w - next + load_cell w, 0(psp) + push w + next defcode "swap", 4, 0, 0x64ED874E, swap, dup - pop w - pop x - push w - push x - next + pop w + pop x + push w + push x + next defcode "drop", 4, 0, 0xA9A58D8C, drop, swap - pop zero - next + pop zero + next defcode "2dup", 3, 0, 0xD330F226, two_dup, drop - load_cell w, 0(psp) - load_cell x, cell(psp) - push x - push w - next + load_cell w, 0(psp) + load_cell x, cell(psp) + push x + push w + next # ----------------------------------------------------------------------------- # math and logic # ----------------------------------------------------------------------------- defcode "+", 1, 0, 0xC4ADC675, plus, two_dup - pop w - pop x - add w, w, x - push w - next + pop w + pop x + add w, w, x + push w + next defcode "aligned", 7, 0, 0xC73174DF, aligned, plus - pop w - addi w, w, 3 - andi w, w, 0xFFFFFFFC - push w - next + pop w + addi w, w, 3 + andi w, w, 0xFFFFFFFC + push w + next # ----------------------------------------------------------------------------- # comparison # ----------------------------------------------------------------------------- defcode "0=", 2, 0, 0x14ED5DD6, zero_equal, aligned - pop w - mv x, zero - bnez w, _zero_done - addi x, x, -1 + pop w + mv x, zero + bnez w, _zero_done + addi x, x, -1 _zero_done: - push x - next + push x + next defcode "=", 1, 0, 0x380CAD68, equal, zero_equal - pop w - pop x - bne w, x, _equal_not - push_imm -1 - next + pop w + pop x + bne w, x, _equal_not + push_imm -1 + next _equal_not: - push zero - next + push zero + next # ----------------------------------------------------------------------------- # compiler # ----------------------------------------------------------------------------- defword "create", 6, 0, 0x26BB595D, create, equal - .int latest, fetch, comma # link - .int hash, comma # hash - .int lit, 0, comma # meta - .int lit, 0, char_comma # flags - .int align - .int here, lit, -code_offset, plus, latest, store # update 'latest' - .int exit + .int latest, fetch, comma # link + .int hash, comma # hash + .int lit, 0, comma # meta + .int lit, 0, char_comma # flags + .int align + .int here, lit, -code_offset, plus, latest, store # update 'latest' + .int exit defcode "hash", 4, 0, 0xEDBF0FE3, hash, create - pop x # string length - pop w # string address - jal hash_impl - push w # string hash - next + pop x # string length + pop w # string address + jal hash_impl + push w # string hash + next hash_impl: # 32-bit fnv1a - li t0, 2166136261 # hash - li t1, 16777619 # prime + li t0, 2166136261 # hash + li t1, 16777619 # prime _hash_char: - lb t2, 0(w) - addi w, w, 1 - addi x, x, -1 - xor t0, t0, t2 - mul t0, t0, t1 - bgtz x, _hash_char - mv w, t0 - ret + lb t2, 0(w) + addi w, w, 1 + addi x, x, -1 + xor t0, t0, t2 + mul t0, t0, t1 + bgtz x, _hash_char + mv w, t0 + ret defcode "[", 1, 0, 0xDE0C1FBA, l_bracket, hash - la w, _state - li x, state_immediate - store_cell x, 0(w) - next + la w, _state + li x, state_immediate + store_cell x, 0(w) + next defcode "]", 1, 0, 0xD80C1648, r_bracket, l_bracket - la w, _state - li x, state_compile - store_cell x, 0(w) - next + la w, _state + li x, state_compile + store_cell x, 0(w) + next defcode "hidden", 6, 0, 0xF618F139, hidden, r_bracket - pop w - addi w, w, flag_offset - lb x, 0(w) - xori x, x, flag_hidden - sb x, 0(w) - next + pop w + addi w, w, flag_offset + lb x, 0(w) + xori x, x, flag_hidden + sb x, 0(w) + next defword ":", 1, 0, 0x3F0CB86D, colon, hidden - .int parse_word, create # create header - .int lit, docol, comma # append 'docol' - .int latest, fetch, hidden # hide word - .int r_bracket # enter 'compile' mode - .int exit + .int parse_word, create # create header + .int lit, docol, comma # append 'docol' + .int latest, fetch, hidden # hide word + .int r_bracket # enter 'compile' mode + .int exit defword ";", 1, flag_immediate, 0x3E0CB6DA, semicolon, colon - .int lit, exit, comma # append 'exit' - .int latest, fetch, hidden # show word - .int l_bracket # enter 'immediate' mode - .int exit + .int lit, exit, comma # append 'exit' + .int latest, fetch, hidden # show word + .int l_bracket # enter 'immediate' mode + .int exit defcode "compiling?", 10, 0, 0x94652AE2, compiling_q, semicolon - la w, _state - load_cell w, 0(w) - andi w, w, state_compile - push w - next + la w, _state + load_cell w, 0(w) + andi w, w, state_compile + push w + next defcode "immediate?", 10, 0, 0x89F23E9F, immediate_q, compiling_q - pop w - addi w, w, flag_offset - code_offset - lb w, 0(w) - andi w, w, flag_immediate - push w - next + pop w + addi w, w, flag_offset - code_offset + lb w, 0(w) + andi w, w, flag_immediate + push w + next .section ".text" file_get_char: - la w, _bootstrap - load_cell w, 0(w) - la x, _bootstrap_len - load_cell x, 0(x) - sub x, x, w - la y, _bootstrap_offset - load_cell y, 0(y) - add w, w, y - sub x, x, y - beqz x, _file_eof - - lb w, 0(w) - li t0, 0x0A # '\n' - bne w, t0, _file_got_char - li w, 0x20 + la w, _bootstrap + load_cell w, 0(w) + la x, _bootstrap_len + load_cell x, 0(x) + sub x, x, w + la y, _bootstrap_offset + load_cell y, 0(y) + add w, w, y + sub x, x, y + beqz x, _file_eof + + lb w, 0(w) + li t0, 0x0A # '\n' + bne w, t0, _file_got_char + li w, 0x20 _file_got_char: - addi y, y, 1 - la x, _bootstrap_offset - store_cell y, 0(x) - ret + addi y, y, 1 + la x, _bootstrap_offset + store_cell y, 0(x) + ret _file_eof: - li w, 0x0A - ret + li w, 0x0A + ret .section ".rodata" program: - .int type - .int quit + .int type + .int quit version_string: - .ascii "soup forth rv32\n" - version_string_len = (. - version_string) + .ascii "soup forth rv32\n" + version_string_len = (. - version_string) word_buffer: .space 255 .balign cell @@ -674,15 +674,15 @@ _fib_len: .int 255 .section ".text.boot" start: - la psp, __stacktop - la rsp, __stacktop_ret - la ip, program + la psp, __stacktop + la rsp, __stacktop_ret + la ip, program - push_addr version_string - push_imm version_string_len + push_addr version_string + push_imm version_string_len - push_addr version_string - push_imm version_string_len + push_addr version_string + push_imm version_string_len - next + next