-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsforth.S
More file actions
479 lines (368 loc) · 11.5 KB
/
sforth.S
File metadata and controls
479 lines (368 loc) · 11.5 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
### Scouarn Forth Bootstrap system
# %rbp: data stack pointer
# %r8: top of the stack
## System constants
.equ DSTACK_SZ, 0x04000 # TODO: dynamic allocation using mmap syscall
.equ DICT_SZ, 0x10000 # TODO: dynamic allocation using mmap syscall
.equ TIB_SZ, 256
.equ FLAG_IMM, 0x01
.equ FLAG_HIDDEN, 0x02
.equ STDIN, 0
.equ STDOUT, 1
.equ __NR_read, 0
.equ __NR_write, 1
.equ __NR_exit, 60
## Stacks
.bss
rstack_init: .quad 0 # Return stack initial value
dstack_end:
.skip DSTACK_SZ
dstack_start:
.macro DPUSH src
leaq -8(%rbp), %rbp
movq \src, (%rbp)
.endm
.macro DPOP dst
movq (%rbp), \dst
leaq 8(%rbp), %rbp
.endm
## Dictionary
.equ _LAST_DEF, 0
.macro DEFINE_WORD label, name, flags=0
1:
.quad _LAST_DEF
.byte \flags
.byte (3f - 2f)
2:
.ascii "\name"
3:
xt_\label:
.equ _LAST_DEF, 1b
.endm
.macro DEFINE_CONST label, name, value, flags=0
DEFINE_WORD \label, \name, \flags
DPUSH %r8
mov $\value, %r8
ret
.endm
.section .dictionary, "awx", @progbits
dict_start:
## Entrypoint
.globl _start
_start:
cld
xorq %r8, %r8
movq $dstack_start, %rbp # Data stack
movq %rsp, rstack_init # Return stack
jmp _QUIT # No return
## Consts and variables
DEFINE_CONST SZero, "S0", dstack_start
DEFINE_CONST STATE, "STATE", state
DEFINE_CONST LATEST, "LATEST", last_def
DEFINE_CONST CP, "CP", curr_def
DEFINE_CONST CPZero, "CP0", dict_start
DEFINE_CONST CPOne, "CP1", user_dict_start
DEFINE_CONST brk, "brk", dict_end
dict_end: .quad (user_dict_start+DICT_SZ)
curr_def: .quad user_dict_start
state: .quad 0
DEFINE_WORD RSP0, "RSP0" # ( -- addr )
DPUSH %r8
mov rstack_init, %r8
ret
DEFINE_CONST IN, "IN", source
DEFINE_CONST SupIN, ">IN", source_idx
DEFINE_CONST HashIN, "\#IN", source_cnt
DEFINE_CONST TIB, "TIB", tib
source: .quad tib
source_idx: .quad 0
source_cnt: .quad 0
tib: .skip TIB_SZ
# Compilation
DEFINE_WORD Lsq, "[", FLAG_IMM
movq $0, state
ret
DEFINE_WORD Rsq, "]"
movq $-1, state
ret
DEFINE_WORD Comma, "," # ( x -- )
movq curr_def, %rdi
movq %r8, (%rdi)
addq $8, curr_def
DPOP %r8
ret
DEFINE_WORD CComma, "C," # ( char -- )
movq curr_def, %rdi
movq %r8, %rax
DPOP %r8
movb %al, (%rdi)
incq curr_def
ret
DEFINE_WORD COMPILEComma, "COMPILE," # ( xt -- )
movq %r8, %rax # xt
DPOP %r8
_COMPILE: # ( -- ) %rax : xt
# Compile: call REL
# Where REL is xt relative to the next instruction:
# REL = xt - (curr_def + 5) = %rax - 5 - %rdx
movq curr_def, %rdx
movb $0xe8, (%rdx) # Append opcode of call
subq $5, %rax
subq %rdx, %rax
movl %eax, 1(%rdx) # Append offset
addq $5, curr_def # Update compilation pointer
ret
DEFINE_WORD LITERAL, "LITERAL", FLAG_IMM # ( x -- )
movq %r8, %rax # x
DPOP %r8
_LITERAL: # ( -- ) %rax : x
movq curr_def, %rdx
# TODO: use sign extended for more compact code
# Compile: leaq -8(%rbp), %rbp
# 48 8d 6d f8
movl $0xf86d8d48, (%rdx)
# Compile: movq %r8, (%rbp)
# 4c 89 45 00
movl $0x0045894c, 4(%rdx)
# Compile: movq $IMM, %r8
# 49 b8 xx xx xx xx xx xx xx xx
movw $0xb849, 8(%rdx)
movq %rax, 10(%rdx)
addq $18, curr_def
ret
DEFINE_WORD Colon, ":" # ( "<spaces>ccc<space>" -- )
movq last_def, %rax
movq curr_def, %rdi
movq %rdi, last_def # Update last_def
stosq # Put link field
movb $FLAG_HIDDEN, %al
stosb # Put hidden flag
push %rdi
movb $' ', %bl
call _SKIP
call _PARSE # FIXME: Allow empty word ?
movq %rdi, %rsi # c-addr
mov %rcx, %rax # u
pop %rdi
stosb # Put size
repe movsb # Put name
movq $-1, state # Enter compilation mode
movq %rdi, curr_def # Update CP
ret
DEFINE_WORD Semi, ";", FLAG_IMM
movq curr_def, %rdi
movb $0xc3, (%rdi) # Compile a ret instruction
incq curr_def
movq last_def, %rdi # Make the word visible
andb $(~FLAG_HIDDEN), 8(%rdi)
movq $0, state # Exit compilation mode
ret
DEFINE_WORD IMMEDIATE, "IMMEDIATE", # ( -- )
# Toggle imm flag
movq last_def, %rdi
xorb $FLAG_IMM, 8(%rdi)
ret
## Parsing
_REFILL: # ( -- ) Read line in source buff, update source_idx and source_cnt
movq $0, source_idx
mov $tib, %rdi
.Lrefill_loop:
push %rdi
pushw $0 # Input one char
movq $__NR_read, %rax
movq $STDIN, %rdi
movq %rsp, %rsi
movq $1, %rdx
syscall
test %rax, %rax
popw %ax
pop %rdi
jl .Lrefill_err # Error
je .Lrefill_loop # Nothing read
cmpb $'\r', %al # Line terminator
je .Lrefill_end
cmpb $'\n', %al
je .Lrefill_end
stosb
cmp $(tib+TIB_SZ), %rdi
jb .Lrefill_loop
.Lrefill_end:
sub $tib, %rdi
movq %rdi, source_cnt
ret
.Lrefill_err:
movq $-1, source_cnt
ret
DEFINE_WORD SCAN_UNTIL, "scan-until" # ( char "ccc<char>" -- )
mov %r8, %rbx
DPOP %r8
_PARSE: # bl:char -- rdi:c-addr rcx:u
movq source_idx, %rsi
movq source_cnt, %rdx
movq source, %r15
leaq (%r15,%rsi), %rdi # c-addr
xorq %rcx, %rcx # u
.Lparse_loop: # Scan until end of buffer or <char> is found
cmpq %rdx, %rsi
jge .Lparse_end
movb (%r15,%rsi), %al
inc %rsi
inc %rcx
cmpb %al, %bl
jne .Lparse_loop
dec %rcx
.Lparse_end:
movq %rsi, source_idx
ret
DEFINE_WORD SCAN_WHILE, "scan-while" # ( char "<chars>ccc" -- "ccc" )
mov %r8, %rbx
DPOP %r8
_SKIP: # bl:char
movq source_idx, %rsi
movq source_cnt, %rdx
movq source, %r15
.Lskip_loop:
cmpq %rdx, %rsi
jge .Lskip_end
cmpb $' ', (%r15,%rsi)
jne .Lskip_end
incq %rsi
jmp .Lskip_loop
.Lskip_end:
movq %rsi, source_idx
ret
DEFINE_WORD PARSE "PARSE" # ( char "ccc<char>" -- c-addr u )
call xt_SCAN_UNTIL
DPUSH %r8
DPUSH %rdi # c-addr
movq %rcx, %r8 # u
ret
_NUMBER: # ( rdi:c-addr rdx:u -- rax:u rbx:flag ) Parse hex number
xorq %rsi, %rsi # Index in word_buf
xorq %rax, %rax # Result
movq $0, %rbx # FALSE (failure)
.Lnumber_loop:
movzxb (%rdi,%rsi), %rcx
incq %rsi
cmpb $'9', %cl
jbe .Lnumber_digit
cmpb $'A', %cl
jb .Lnumber_fail
subb $('A'-'0'-10), %cl
.Lnumber_digit:
subb $'0', %cl
cmpb $0x0f, %cl
ja .Lnumber_fail
shlq $4, %rax # Multiply by 15
addq %rcx, %rax # Add digit
cmpq %rdx, %rsi
jb .Lnumber_loop
decq %rbx # TRUE (ok)
.Lnumber_fail:
ret
## Interpreter
DEFINE_WORD BYE, "BYE"
movq $__NR_exit, %rax
movq $0, %rdi
syscall
_QUIT:
movq rstack_init, %rsp # Empty the return stack
movq $0, state # Interpreter mode
.Lquit_loop:
call _REFILL
cmpq $0, source_cnt
jl xt_BYE
call _INTERPRET
jmp .Lquit_loop
_FIND: # ( rsi:c-addr rax:u -- rbx:0|nt rdi:xt dl:flags )
movq last_def, %rbx
jmp .L_find_begin
.L_find_loop:
movq (%rbx), %rbx # Next
.L_find_begin:
test %rbx, %rbx
jz .L_find_not_found
movzxb 8(%rbx), %rdx # Flags
test $FLAG_HIDDEN, %dl
jnz .L_find_loop # Hidden word
movzxb 9(%rbx), %rcx # Length
# jrcxz .L_find_loop # Ignore if length is zero (:NONAME) ## Is it even useful to check for that ?
cmp %rax, %rcx
jne .L_find_loop # Next word if the length doesn't match
leaq 10(%rbx), %rdi
push %rsi
repe cmpsb # String compare
pop %rsi
jne .L_find_loop # Next word if not equal
# Found ! After repe, %rdi contains the xt
.L_find_not_found:
ret
DEFINE_WORD find, "find" # ( c-addr u -- xt flags 0|nt )
movq %r8, %rax
movq (%rbp), %rsi
call _FIND
lea -8(%rbp), %rbp
movq %rdi, 8(%rbp) # xt
movq %rdx, (%rbp) # flag
movq %rbx, %r8 # nt
ret
_INTERPRET:
.Linterpret_loop:
movb $' ', %bl
call _SKIP
call _PARSE
test %rcx, %rcx
jnz .Linterpret_try_find
ret
.Linterpret_try_find:
push %rdi # c-addr
push %rcx # u
movq %rdi, %rsi # c-addr
movq %rcx, %rax # u
call _FIND
test %rbx, %rbx
jz .Linterpret_try_number
addq $16, %rsp # Drop
test $FLAG_IMM, %dl
jnz .Linterpret_exec_word
cmpq $0, state
jz .Linterpret_exec_word
movq %rdi, %rax
call _COMPILE # Compile xt
jmp .Linterpret_loop
.Linterpret_exec_word:
call *%rdi # Execute xt
jmp .Linterpret_loop
.Linterpret_try_number:
movq 0(%rsp), %rdx # u
movq 8(%rsp), %rdi # c-addr
call _NUMBER
test %rbx, %rbx
jz .Linterpret_not_found
addq $16, %rsp # Drop
cmpq $0, state
jz .Linterpret_exec_number
call _LITERAL # Compile number
jmp .Linterpret_loop
.Linterpret_exec_number:
DPUSH %r8
movq %rax, %r8 # Push number
jmp .Linterpret_loop
.Linterpret_not_found:
movq $__NR_write, %rax # Print error message
movq $STDOUT, %rdi
movq $.Linterpret_msg, %rsi
movq $.Linterpret_msg_sz, %rdx
syscall
movq $__NR_write, %rax # Print word
movq $STDOUT, %rdi
pop %rdx # u
pop %rsi # c-addr
syscall
jmp xt_BYE # No return
.Linterpret_msg:
.ascii "**NOT FOUND** "
.equ .Linterpret_msg_sz, (.-.Linterpret_msg)
last_def: .quad _LAST_DEF
user_dict_start:
.skip DICT_SZ