acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

harlan/rv-forth

Public
Star5

A tiny Forth kernel for RV64: Linux and QEMU virt builds, linked from handwritten assembly.

Code
Blame·1 commit across 233 linesoldernewer
First runnable RV64 Forth kernelOct 11 1// forth.S: an indirect-threaded Forth kernel for RV64.
2//
3// s0 IP the next cell of threaded code
4// s1 DSP data stack, full descending, one 64-bit cell per item
5// s2 RSP return stack, full descending
6//
7// A word is a codeword (the address of machine code) followed by its body.
8// Code words point at their own assembly; colon definitions point at DOCOL,
9// which saves IP and starts interpreting the body.
10//
11// The board lives elsewhere. virt.S boots QEMU virt bare-metal and linux.S
12// runs the kernel as a Linux process. Each one supplies _start, which sets up
13// sp and jumps to forth_start, and putc, getc and halt.
14
15// --- the inner interpreter ---------------------------------------------------
16
17 .macro NEXT
18 ld t0, 0(s0) // t0 = execution token (address of a codeword)
19 addi s0, s0, 8
20 ld t1, 0(t0) // the codeword: where its machine code lives
21 jr t1
22 .endm
23
24 .macro PUSHRSP reg
25 addi s2, s2, -8
26 sd \reg, 0(s2)
27 .endm
28
29 .macro POPRSP reg
30 ld \reg, 0(s2)
31 addi s2, s2, 8
32 .endm
33
34 .macro PUSH reg
35 addi s1, s1, -8
36 sd \reg, 0(s1)
37 .endm
38
39 .macro POP reg
40 ld \reg, 0(s1)
41 addi s1, s1, 8
42 .endm
43
44// A primitive: an execution token in .rodata whose codeword points at the code below it.
45 .macro defcode label
46 .section .rodata
47 .p2align 3
48\label:
49 .dword code_\label
50 .text
51 .p2align 2
52code_\label:
53 .endm
54
55// A colon definition: an execution token whose codeword is DOCOL; the body follows.
56 .macro defword label
57 .section .rodata
58 .p2align 3
59\label:
60 .dword DOCOL
61 .endm
62
63 .text
64 .p2align 2
65 .global forth_start
66forth_start:
67 la s1, dstack_top
68 la s2, rstack_top
69 la s0, cold
70 NEXT
71
72DOCOL:
73 PUSHRSP s0
74 addi s0, t0, 8 // the body starts after the codeword
75 NEXT
76
77// --- primitives ----------------------------------------------------------------
78
79 defcode EXIT
80 POPRSP s0
81 NEXT
82
83 defcode LIT // ( -- n ) the next cell is a number
84 ld t2, 0(s0)
85 addi s0, s0, 8
86 PUSH t2
87 NEXT
88
89 defcode DUP // ( a -- a a )
90 ld t2, 0(s1)
91 PUSH t2
92 NEXT
93
94 defcode DROP // ( a -- )
95 addi s1, s1, 8
96 NEXT
97
98 defcode SWAP // ( a b -- b a )
99 ld t2, 0(s1)
100 ld t3, 8(s1)
101 sd t3, 0(s1)
102 sd t2, 8(s1)
103 NEXT
104
105 defcode OVER // ( a b -- a b a )
106 ld t2, 8(s1)
107 PUSH t2
108 NEXT
109
110 defcode ADD // ( a b -- a+b )
111 POP t2
112 ld t3, 0(s1)
113 add t3, t3, t2
114 sd t3, 0(s1)
115 NEXT
116
117 defcode SUB // ( a b -- a-b )
118 POP t2
119 ld t3, 0(s1)
120 sub t3, t3, t2
121 sd t3, 0(s1)
122 NEXT
123
124 defcode MUL // ( a b -- a*b )
125 POP t2
126 ld t3, 0(s1)
127 mul t3, t3, t2
128 sd t3, 0(s1)
129 NEXT
130
131 defcode FETCH // ( addr -- n )
132 ld t2, 0(s1)
133 ld t2, 0(t2)
134 sd t2, 0(s1)
135 NEXT
136
137 defcode STORE // ( n addr -- )
138 POP t2
139 POP t3
140 sd t3, 0(t2)
141 NEXT
142
143 defcode BRANCH // the next cell is a byte offset from itself
144 ld t2, 0(s0)
145 add s0, s0, t2
146 NEXT
147
148 defcode ZBRANCH // ( flag -- ) branch when flag is zero
149 POP t3
150 beqz t3, code_BRANCH
151 addi s0, s0, 8 // skip the offset
152 NEXT
153
154 defcode EMIT // ( c -- )
155 POP a0
156 call putc
157 NEXT
158
159 defcode KEY // ( -- c )
160 call getc
161 PUSH a0
162 NEXT
163
164 defcode DOT // ( n -- ) print as signed decimal
165 POP a0
166 call print_dec
167 NEXT
168
169 defcode HALT
170 tail halt
171
172// --- colon definitions -----------------------------------------------------------
173
174 defword SQUARE // ( n -- n*n )
175 .dword DUP, MUL, EXIT
176
177// cold: print the squares of 1 to 10, then halt.
178 .section .rodata
179 .p2align 3
180cold:
181 .dword LIT, 1
182.Lloop:
183 .dword DUP, SQUARE, DOT
184 .dword LIT, 1, ADD
185 .dword DUP, LIT, 11, SUB
186 .dword ZBRANCH, .Ldone - .
187 .dword BRANCH, .Lloop - .
188.Ldone:
189 .dword DROP, LIT, 10, EMIT, HALT
190
191// --- printing ----------------------------------------------------------------------
192
193 .text
194 .p2align 2
195print_dec: // a0 = signed value; prints digits and a space
196 addi sp, sp, -48
197 sd ra, 40(sp)
198 sd s4, 32(sp)
199 sd s5, 24(sp)
200 mv s4, a0
201 bgez s4, 1f
202 li a0, '-'
203 call putc
204 neg s4, s4 // INT64_MIN stays INT64_MIN; divu reads it as 2^63
2051: addi s5, sp, 24 // digits are written backwards into sp+0..23
206 li t2, 10
2072: remu t3, s4, t2
208 divu s4, s4, t2
209 addi t3, t3, '0'
210 addi s5, s5, -1
211 sb t3, 0(s5)
212 bnez s4, 2b
2133: lbu a0, 0(s5)
214 call putc
215 addi s5, s5, 1
216 addi t4, sp, 24
217 bltu s5, t4, 3b
218 li a0, ' '
219 call putc
220 ld s5, 24(sp)
221 ld s4, 32(sp)
222 ld ra, 40(sp)
223 addi sp, sp, 48
224 ret
225
226// --- stacks ----------------------------------------------------------------------------
227
228 .section .bss
229 .p2align 4
230 .space 4096
231dstack_top:
232 .space 4096
233rstack_top: