summaryrefslogtreecommitdiff
path: root/jefs.s
blob: ac649c13c8d9cb0a26991d8e5c43d62c58395df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
; jewelforth

;; MACROS {{{
%macro pspush 1
	lea r14, [r14-8]
	mov qword [r14], %1
%endmacro

%macro pspop 1
	mov %1, qword [r14]
	lea r14, [r14+8]
%endmacro

;;; dictionary macros {{{
%define mac_latest	0	; updated through defdict

%macro defdict 3 ; name asm-label flags
	%strlen slen %1
	global lfa_%2
	lfa_%2: dq mac_latest
		%define mac_latest lfa_%2
	ffa_%2:	db %3 ; FFA
	nfa_%2:	dw slen ; NFA
		db %1
		db 0
%endmacro

%macro defword 3
	defdict %1, %2, %3
	%2:
%endmacro

%macro defconst 4 ; ... value
	defdict %1, %2, %3
	%define %2 %4
	pspush qword %4
	ret
%endmacro

%macro defvar 4 ; ... default-value
	%2: dq %4
	defdict %1, %2, %3
	pspush qword %2
	ret
%endmacro

%assign smudge_mask	0x1
%assign immediate_mask	0x2

%assign false 0x0
%assign true 0xffffffffffffffff

%assign interpreting 0x0
%assign compiling 0xffffffffffffffff
;;; }}}

;; syscall
%assign	__NR_read	0
%assign	__NR_write	1
%assign	__NR_brk	12
%assign	__NR_exit	60
;; }}}

section .bss
wstack_b:	resq 2047
wstack:		resq 1

section .text
global _start
_start:
	; init
	mov r14, wstack ; point SP to top

	; EXPAND BRK
	xor rdi, rdi ; brk syscall called with 0 gives the current brk
	mov rax, __NR_brk
	syscall
	mov qword [here], rax
	mov qword [h0], rax

	add rax, 0x9c400 ; 640kb, entirely arbitrary
	mov rdi, rax
	mov rax, __NR_brk
	syscall ; if this fails, have fun
	mov qword [hend], rax

	call interpret
	mov rdi, 0
	mov rax, __NR_exit
	syscall

defword "bye", bye, 0
	mov rdi, 0
	mov rax, __NR_exit
	syscall
	ret ; will not be reached

defword "@", fetch, 0
	pspop r11
	mov r12, qword [r11]
	pspush r12
	ret

defword "c@", cfetch, 0
	pspop r11
	xor r12, r12
	mov r12b, [r11]
	pspush r12
	ret

defword "!", store, 0
	pspop r11
	pspop r12
	mov qword [r11], r12
	ret

defword "c!", cstore, 0
	pspop r11
	pspop r12
	mov [r11], r12b
	ret

; stage 1 parser. very rudimentary, does not recognise numbers,
; only recognises newlines and spaces as whitespace.
; since it will only parse a little bit of the init file
; there won't be any error checking either.
defword "parse", parse, 0
	mov r13, qword [to_in]
	add r13, initfile
	mov r12b, byte [r13]

.wsloop: ; skip initial ws
	cmp r12b, 0x20
	je .wsloop_cont
	cmp r12b, 0x0a
	jne .wordloop_start
.wsloop_cont:
	inc r13
	mov r12b, byte [r13]
	jmp .wsloop

.wordloop_start:
	push r13 ; keep start address of this word for later
	mov r11, 1 ; W: word length count
.wordloop:
	cmp r12b, 0x20
	je .wordloop_end
	cmp r12b, 0x0a
	je .wordloop_end

	inc r11
	inc r13
	mov r12b, byte [r13]
	jmp .wordloop

.wordloop_end:
	dec r11
	sub r13, initfile
	mov qword [to_in], r13
	pop r13
	pspush r13 ; c-addr
	pspush r11 ; u
	ret

; probably fucked up and broken. have not tested any of this yet
defword "find", find, 0
	pspop r10 ; u
	pspop r11 ; c-addr
	mov r12, latest
	mov r13, qword [r12]
	mov r12, r13

.check_smudge:
	add r13, 8
	mov r9b, byte [r13]
	test r9b, smudge_mask
	jnz .no

.no_smudge:
	inc r13
	mov r9w, word [r13]
	cmp r9w, r10w
	jne .no
	mov rsi, r13
	add rsi, 2
	mov rdi, r11
	mov rcx, r10
	repz cmpsb
	jnz .no
	sub r13, 9
	pspush r13
	mov r13, true
	pspush r13
	ret

.no:
	mov r13, qword [r12]
	mov r12, r13
	cmp r13, 0 ; end of dictionary? fallthrough to notfound if so
	jne .check_smudge

	mov r13, false
	pspush r13
	ret

; stage 1 interpreter, just reads from initfile
defword "interpret", interpret, 0
.loop:
	call parse
	call find
	pspop r11 ; assume it exists for testing
	call to_cfa
	pspop r11
	call r11
	ret

defword ">cfa", to_cfa, 0
	pspop r11
	add r11, 9
	xor r12, r12
	mov r12w, word [r11]
	add r11, 2
	add r11, r12
	inc r11
	pspush r11
	ret

defword "compile,", compile_comma, 0
	pspop r11
	mov r12, [here]

	; compile mov r11, [cfa]
	mov word [r12], 0x94bb
	add r12, 2
	mov qword [r12], r11
	add r12, 8

	; compile call r11
	mov word [r12], 0x41ff
	add r12, 2
	mov byte [r12], 0xd3
	inc r12

	mov qword [here], r12
	ret

defword ":", colon, 0
	ret

defword ";", semicolon, immediate_mask
	ret

defvar ">in", to_in, 0, 0
defvar "state", state, 0, interpreting
defvar "here", here, 0, 0
defvar "h0", h0, 0, 0 ; beginning of user memory area
defvar "hend", hend, 0, 0 ; ending of user memory area
defvar "latest", latest, 0, lfa_latest

initfile:
incbin "jefs.fs"
initlen equ $ - initfile
initfile_end: