// forth.S: an indirect-threaded Forth kernel for RV64. // // s0 IP the next cell of threaded code // s1 DSP data stack, full descending, one 64-bit cell per item // s2 RSP return stack, full descending // // A word is a codeword (the address of machine code) followed by its body. // Code words point at their own assembly; colon definitions point at DOCOL, // which saves IP and starts interpreting the body. // // The board lives elsewhere. virt.S boots QEMU virt bare-metal and linux.S // runs the kernel as a Linux process. Each one supplies _start, which sets up // sp and jumps to forth_start, and putc, getc and halt. // --- the inner interpreter --------------------------------------------------- .macro NEXT ld t0, 0(s0) // t0 = execution token (address of a codeword) addi s0, s0, 8 ld t1, 0(t0) // the codeword: where its machine code lives jr t1 .endm .macro PUSHRSP reg addi s2, s2, -8 sd \reg, 0(s2) .endm .macro POPRSP reg ld \reg, 0(s2) addi s2, s2, 8 .endm .macro PUSH reg addi s1, s1, -8 sd \reg, 0(s1) .endm .macro POP reg ld \reg, 0(s1) addi s1, s1, 8 .endm // A primitive: an execution token in .rodata whose codeword points at the code below it. .macro defcode label .section .rodata .p2align 3 \label: .dword code_\label .text .p2align 2 code_\label: .endm // A colon definition: an execution token whose codeword is DOCOL; the body follows. .macro defword label .section .rodata .p2align 3 \label: .dword DOCOL .endm .text .p2align 2 .global forth_start forth_start: la s1, dstack_top la s2, rstack_top la s0, cold NEXT DOCOL: PUSHRSP s0 addi s0, t0, 8 // the body starts after the codeword NEXT // --- primitives ---------------------------------------------------------------- defcode EXIT POPRSP s0 NEXT defcode LIT // ( -- n ) the next cell is a number ld t2, 0(s0) addi s0, s0, 8 PUSH t2 NEXT defcode DUP // ( a -- a a ) ld t2, 0(s1) PUSH t2 NEXT defcode DROP // ( a -- ) addi s1, s1, 8 NEXT defcode SWAP // ( a b -- b a ) ld t2, 0(s1) ld t3, 8(s1) sd t3, 0(s1) sd t2, 8(s1) NEXT defcode OVER // ( a b -- a b a ) ld t2, 8(s1) PUSH t2 NEXT defcode ADD // ( a b -- a+b ) POP t2 ld t3, 0(s1) add t3, t3, t2 sd t3, 0(s1) NEXT defcode SUB // ( a b -- a-b ) POP t2 ld t3, 0(s1) sub t3, t3, t2 sd t3, 0(s1) NEXT defcode MUL // ( a b -- a*b ) POP t2 ld t3, 0(s1) mul t3, t3, t2 sd t3, 0(s1) NEXT defcode FETCH // ( addr -- n ) ld t2, 0(s1) ld t2, 0(t2) sd t2, 0(s1) NEXT defcode STORE // ( n addr -- ) POP t2 POP t3 sd t3, 0(t2) NEXT defcode BRANCH // the next cell is a byte offset from itself ld t2, 0(s0) add s0, s0, t2 NEXT defcode ZBRANCH // ( flag -- ) branch when flag is zero POP t3 beqz t3, code_BRANCH addi s0, s0, 8 // skip the offset NEXT defcode EMIT // ( c -- ) POP a0 call putc NEXT defcode KEY // ( -- c ) call getc PUSH a0 NEXT defcode DOT // ( n -- ) print as signed decimal POP a0 call print_dec NEXT defcode HALT tail halt // --- colon definitions ----------------------------------------------------------- defword SQUARE // ( n -- n*n ) .dword DUP, MUL, EXIT // cold: print the squares of 1 to 10, then halt. .section .rodata .p2align 3 cold: .dword LIT, 1 .Lloop: .dword DUP, SQUARE, DOT .dword LIT, 1, ADD .dword DUP, LIT, 11, SUB .dword ZBRANCH, .Ldone - . .dword BRANCH, .Lloop - . .Ldone: .dword DROP, LIT, 10, EMIT, HALT // --- printing ---------------------------------------------------------------------- .text .p2align 2 print_dec: // a0 = signed value; prints digits and a space addi sp, sp, -48 sd ra, 40(sp) sd s4, 32(sp) sd s5, 24(sp) mv s4, a0 bgez s4, 1f li a0, '-' call putc neg s4, s4 // INT64_MIN stays INT64_MIN; divu reads it as 2^63 1: addi s5, sp, 24 // digits are written backwards into sp+0..23 li t2, 10 2: remu t3, s4, t2 divu s4, s4, t2 addi t3, t3, '0' addi s5, s5, -1 sb t3, 0(s5) bnez s4, 2b 3: lbu a0, 0(s5) call putc addi s5, s5, 1 addi t4, sp, 24 bltu s5, t4, 3b li a0, ' ' call putc ld s5, 24(sp) ld s4, 32(sp) ld ra, 40(sp) addi sp, sp, 48 ret // --- stacks ---------------------------------------------------------------------------- .section .bss .p2align 4 .space 4096 dstack_top: .space 4096 rstack_top: