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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
; sanctuary
; macros {{{
; TODO: error handling (once i add that)
%macro pspush 1
lea r15, [r15-8]
mov qword [r15], %1
%endmacro
%macro pspop 1
mov %1, qword [r15]
lea r15, [r15+8]
%endmacro
%define s_latest 0
%macro defdict 3 ; name label flags
%strlen slen %1
global lfa_%2
lfa_%2: dq s_latest
%define s_latest lfa_%2
ffa_%2: db %3
nfa_%2: db slen
db %1
%endmacro
%macro defcode 3
defdict %1, %2, %3
%2:
%endmacro
; this is just taken from jewelforth, and does not correspond
; to how user variables are planned to work in sanctuary
; so todo make better later? i don't know if it really matters
; because it will only apply to builtin variables.
%macro defvar 4
%2: dq %4
defdict %1, %2, %3
pspush qword %2
%endmacro
; }}}
%assign smudge_mask 0x1
%assign immediate_mask 0x2
%assign comp_only_mask 0x4
%assign true 0x0
%assign false (~0x0)
%assign INTERPRET 0x0
%assign COMPILING (~0x0)
%assign __NR_write 1
%assign __NR_mprotect 10
%assign __NR_brk 12
%assign __NR_exit 60
section .bss
resq 4091
wstk: resq 1
section .text
global _start
_start:
lea r15, [wstk]
call brk@
pspop r11
mov qword [dp], r11
mov qword [dp0], r11
mov r11, 0x9c400
pspush r11
call grow
call interpret
call bye
defcode "brk@", brk@, 0
xor rdi, rdi
mov rax, __NR_brk
syscall
pspush rax
ret
defcode "grow", grow, 0
call brk@
pspop rdi
pspop r13
add rdi, r13
mov rax, __NR_brk
syscall
mov qword [dp$], rax
ret
defcode "executable", executable, 0
mov rdx, 0x7 ; PROT_{READ,WRITE,EXEC}
pspop rsi
pspop rdi ; addr
mov rax, __NR_mprotect
syscall
ret
defcode "here", here, 0
mov r11, qword [dp]
pspush r11
ret
defcode "bye", bye, 0
mov rdi, 0
mov rax, __NR_exit
syscall
ret
; input parsing {{{
; r11: string character count
; rsi: input buffer address
; al: char being parsed
; r10: end of input buffer
defcode "parse-name", parse_name, 0
mov rsi, qword [to_in]
mov r10, qword [tib]
add rsi, r10
add r10, qword [n_tib]
xor rax, rax
.wsloop:
cmp rsi, r10
jge .empty
lodsb
cmp al, 0x20
je .wsloop
cmp al, 0x09
je .wsloop
cmp al, 0x0a
je .wsloop
cmp rsi, r10
jge .empty
mov r11, 1
dec rsi ; bring down by one to point to the start
push rsi ; will become `a`
inc rsi
.wordloop:
cmp al, 0x20
je .wordloop_e
cmp al, 0x09
je .wordloop_e
cmp al, 0x0a
je .wordloop_e
cmp rsi, r10
jge .wordloop_e
inc r11
lodsb
jmp .wordloop
.wordloop_e:
dec r11
sub rsi, qword [tib]
mov qword [to_in], rsi
pop rsi
pspush rsi
pspush r11
ret
.empty:
pspush 0
pspush 0
ret
; r11: string character count
; rsi: input buffer address
; al: char being parsed
; r10: end of input buffer
defcode "parse", parse, 0
pspop rbx
mov rsi, qword [to_in]
mov r10, qword [tib]
add rsi, r10
add r10, qword [n_tib]
xor rax, rax
.wsloop:
cmp rsi, r10
jge .empty
lodsb
cmp al, bl
je .wsloop
cmp al, 0x0a
je .wsloop
cmp rsi, r10
jge .empty
mov r11, 1
dec rsi ; bring down by one to point to the start
push rsi ; will become `a`
inc rsi
.wordloop:
cmp al, bl
je .wordloop_e
cmp al, 0x0a
je .wordloop_e
cmp rsi, r10
jge .wordloop_e
inc r11
lodsb
jmp .wordloop
.wordloop_e:
dec r11
sub rsi, qword [tib]
mov qword [to_in], rsi
pop rsi
pspush rsi
pspush r11
ret
.empty:
pspush 0
pspush 0
ret
; }}}
defcode "type", type, 0
pspop rsi
pspop rdx
mov rdi, 1
mov rax, __NR_write
syscall
ret
; r9: processing temporary value
; r10: input size
; r11: input addr
; r12: pointer into currently processing word
; r13: same as r12 but kept at xt
defcode "find", find, 0
pspop r10 ; u
pspop r11 ; a
mov r12, qword [latest]
mov r13, r12
.check_smudge:
add r12, 8
mov r9b, byte [r12]
test r9b, smudge_mask
jnz .no
inc r12
mov r9b, byte [r12]
cmp r9b, r10b
jne .no
mov rsi, r12
inc rsi
mov rdi, r11
mov rcx, r10
repe cmpsb
jnz .no
pspush r13
mov r13, true
pspush r13
ret
.no:
mov r12, qword [r13]
mov r13, r12
cmp r12, 0
jne .check_smudge
pspush r11
pspush r10
mov r13, false
pspush r13
ret
; interpret {{{
; r11: word found flag
; r12: state
; TODO respect comp-only flag (do this once error handling is impld)
defcode "interpret", interpret, 0
.loop:
call parse_name
cmp qword [r15], 0
je .eof
call find
mov r12, qword [state]
cmp r12, INTERPRET
jne .compl
pspop r11
test r11, r11 ; set SF if negative (word found)
js .intrpnum
.callw: ; label here for immed jump
call to_body
pspop r14
call r14
jmp .loop
.intrpnum:
call number
pspop r11
test r11, r11
js .notfound
jmp .loop
.compl:
pspop r11
test r11, r11
js .complnum
; dup immediate? if [imm] else normal then
call dup
call immediate_q
pspop r13
test r13, r13
jns .callw
call to_body
call compile_comma
jmp .loop
.complnum:
call number
pspop r11
test r11, r11
js .notfound
call literal
jmp .loop
.notfound:
; error handling should go here
jmp .loop
.eof:
lea r15, [r15+16] ; drop a u
ret
; }}}
defcode "immediate?", immediate_q, 0
pspop r11
add r11, 8
mov r12b, byte [r11]
xor r13, r13
test r12b, immediate_mask
ret
defcode ">body", to_body, 0
pspop r11
add r11, 9
xor r12, r12
mov r12b, byte [r11]
inc r11
add r11, r12
pspush r11
ret
defcode "literal", literal, immediate_mask|comp_only_mask
; 4d 8d 7f f8
; 49 bb VAL
; 4d 89 1f
pspop r11
mov r12, qword [here]
mov dword [r12], 0xf87f8d4d
add r12, 4
mov word [r12], 0xbb49
add r12, 2
mov qword [r12], r11
add r12, 8
mov word [r12], 0x894d
add r12, 2
mov byte [r12], 0x1f
inc r12
mov qword [here], r12
ret
defcode "compile,", compile_comma, 0
pspop r11
mov r12, [here]
; compile mov r11, [cfa]
mov word [r12], 0xbb49
add r12, 2
mov qword [r12], r11
add r12, 8
; compile call r11
mov word [r12], 0xff41
add r12, 2
mov byte [r12], 0xd3
inc r12
mov qword [here], r12
ret
defcode "[", lbrac, immediate_mask
mov qword [state], INTERPRET
ret
defcode "]", rbrac, immediate_mask
mov qword [state], COMPILING
ret
; number {{{
defcode "number", number, 0 ; ( c-addr u -- ?n flag )
pspop r11 ; u
pspop r12 ; c-addr
xor r13, r13 ; r13: result
xor r14, r14 ; r14b: current char
xor r10, r10 ; r10: negative flag
mov r9, qword [base]
cmp r11, 0
je .no
mov r14b, byte [r12]
cmp r14b, '-'
jnz .enterloop
mov r10, true
inc r12
dec r11
.loop:
mov r14b, byte [r12]
.enterloop:
; non numeral = goodbye
cmp r14b, 48
jl .no
sub r14b, 48
cmp r14b, 10 ; 48+10: < ':', <= '9'
jl .basecmp
cmp r14b, 17 ; ':' - '@'
jl .no
sub r14b, 7 ; keep 10 so 'A' = 10
cmp r14b, 36 ; < '[' <= 'Z'
jl .basecmp
cmp r14b, 42 ; < 'a'
jl .no
sub r14b, 32
cmp r14b, 36 ; < '{' <= 'z'
jl .basecmp
jmp .no
.basecmp:
cmp r14, r9
jge .no
imul r13, r9
add r13, r14
inc r12
dec r11
cmp r11, 0
jne .loop
test r10, r10
jz .bye
neg r13
.bye:
pspush r13
mov r13, true
pspush r13
ret
.no:
mov r13, false
pspush r13
ret
; }}}
defcode "dup", dup, 0
mov r11, [r15]
pspush r11
ret
; .s {{{
defcode ".s", dots, 0
push r11
push r12
mov r12, r15
.loop:
cmp r12, wstk
jge .done
mov [.space], r12
mov rdx, 8 ; qword
mov rsi, .space
mov rdi, 1
mov rax, __NR_write
syscall
mov rdx, 8 ; qword
mov rsi, r12
mov rdi, 1
mov rax, __NR_write
syscall
lea r12, [r12+8]
jmp .loop
.done:
mov rdx, 16 ; 2 qword
mov rsi, .dmsg
mov rdi, 1
mov rax, __NR_write
syscall
pop r12
pop r11
ret
.space: resq 1
.dmsg: db "DONEDONEYIPPEEEE"
; }}}
defvar "state", state, 0, INTERPRET
defvar "base", base, 0, 10
defvar "dp", dp, 0, 0
defvar "dp0", dp0, 0, 0
defvar "dp$", dp$, 0, 0
defvar "tib", tib, 0, initfile
defvar "#tib", n_tib, 0, initlen
defvar ">in", to_in, 0, 0
defvar "latest", latest, 0, lfa_latest
initfile: incbin "sanctuary.fs"
initlen equ $ - initfile
|