summaryrefslogtreecommitdiff
path: root/sanctuary.s
blob: 44cd4afecf7ccb659d9427b806dc7740e2dd8228 (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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
; 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 false 0x0
%assign true (~0x0)

%assign INTERPRET 0x0
%assign COMPILING (~0x0)

%assign __NR_write	1
%assign __NR_mprotect	10
%assign __NR_brk	12
%assign __NR_exit	60

%assign init_brk	0x9c400

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, init_brk
	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
	; this marks the *whole user memory* as executable.
	; technically redundant but i think it's fine
	mov r14, qword [dp0]
	pspush r14
	sub rax, r14
	pspush rax
	call executable
	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)
; TODO it doesnt work right
defcode "interpret", interpret, 0
.loop:
	call parse_name
	cmp qword [r15], 0
	je .eof
	call find
	mov r12, qword [state]
	cmp r12, COMPILING
	je .compl
 
	pspop r11
	cmp r11, false
	je .intrpnum

.callw: ; label here for immed jump
	call to_body
	pspop r13
	call r13
	jmp .loop

.intrpnum:
	call number
	pspop r11
	cmp r11, false
	je .notfound
	jmp .loop

.compl:
	; call dots
	pspop r11
	cmp r11, false
	je .complnum

	; dup immediate? if [imm] else normal then
	; i think this is broken (; not working)
	call dup
	; call dots
	call immediate_q
	; call dots
	pspop r13
	cmp r13, true
	je .callw

	call to_body
	call compile_comma
	jmp .loop

.complnum:
	; call dots
	call number
	; call dots
	pspop r11
	cmp r11, false
	je .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
	setnz r13b
	neg r13
	pspush r13
	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 [dp]

	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 [dp], r12
	ret

defcode "compile,", compile_comma, 0
	pspop r11
	mov r12, [dp]

	; 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 [dp], r12
	ret

defcode "[", lbrac, immediate_mask
	mov qword [state], INTERPRET
	ret

defcode "]", rbrac, immediate_mask
	mov qword [state], COMPILING
	ret

defcode "(header)", brac_header, 0
	pspop r9
	pspop r10
	mov r11, qword [latest]
	mov r12, qword [dp]
	push r12

	mov qword [r12], r11
	add r12, 8
	mov byte [r12], 0
	inc r12
	mov byte [r12], r9b
	inc r12

	mov rcx, r9
	mov rsi, r10
	mov rdi, r12
	rep movsb
	add r12, r9

	mov qword [dp], r12
	pop r12
	pspush r12
	ret

; fix to follow ans: yielding colon-sys
defcode ":", colon, 0
	call parse_name
	; todo check zero
	call brac_header
	pspop r11
	mov qword [latest], r11
	call smudge
	mov qword [state], COMPILING
	ret

; fix to follow ans: reading from colon-sys
; this will not work with :noname or i think does>.
defcode ";", semicolon, immediate_mask
	mov r12, [latest]
	add r12, 8
	mov r13b, [r12]
	and r13b, ~smudge_mask ; does this syntax work? equiv SMUDGEMASK NOT
	mov byte [r12], r13b

	mov r11, qword [dp]
	mov byte [r11], 0xc3 ; ret
	inc r11
	mov qword [dp], r11
	mov qword [state], INTERPRET
	ret

defcode "smudge", smudge, 0
	mov r12, [latest]
	add r12, 8
	mov r13b, [r12]
	xor r13b, smudge_mask
	mov byte [r12], r13b
	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
; }}}

; syscall {{{
defcode "syscall0", syscall0, 0
	pspop rax
	syscall
	pspush rax
	ret

defcode "syscall1", syscall1, 0
	pspop rax
	pspop rdi
	syscall
	pspush rax
	ret

defcode "syscall2", syscall2, 0
	pspop rax
	pspop rdi
	pspop rsi
	syscall
	pspush rax
	ret

defcode "syscall3", syscall3, 0
	pspop rax
	pspop rdi
	pspop rsi
	pspop rdx
	syscall
	pspush rax
	ret
; }}}

; stack {{{
defcode "dup", dup, 0
	mov r11, [r15]
	pspush r11
	ret

defcode "2dup", twodup, 0
	mov r11, [r15]
	mov r12, [r15+8]
	pspush r12
	pspush r11
	ret

defcode "swap", swap, 0
	pspop r11
	pspop r12

	pspush r11
	pspush r12
	ret

defcode "over", over, 0
	mov r11, [r15+8]
	pspush r11
	ret

defcode "rot", rot, 0
	pspop r11
	pspop r12
	pspop r13
	pspush r12
	pspush r11
	pspush r13
	ret

defcode "-rot", dash_rot, 0
	pspop r11
	pspop r12
	pspop r13
	pspush r11
	pspush r13
	pspush r12
	ret

defcode "r>", from_r, 0
	pop r12 ; keep return address
	pop r11
	pspush r11
	push r12
	ret

defcode ">r", to_r, 0
	pop r12 ; ret addr
	pspop r11
	push r11
	push r12
	ret

defcode "drop", drop, 0
	pspop r11
	ret

defcode "2drop", twodrop, 0
	pspop r11
	pspop r11
	ret

defcode "rdrop", rdrop, 0
	pop r12 ; ret addr
	pop r11
	push r12
	ret
; }}}

; math + comparison {{{
; i believe some of these could be improved by direct accesses to [r15]
defcode "+", plus, 0
	pspop r11
	pspop r12
	add r11, r12
	pspush r11
	ret

defcode "-", minus, 0
	pspop r11
	pspop r12
	sub r12, r11
	pspush r12
	ret

defcode "*", _times, 0
	pspop r11
	pspop r12
	imul r11, r12
	pspush r11
	ret

defcode "/mod", divmod, 0
	xor rdx, rdx
	pspop r11
	pspop rax
	idiv r11
	pspush rdx
	pspush rax
	ret

defcode "and", _and, 0
	pspop r11
	and [r14], r11
	ret

defcode "or", _or, 0
	pspop r11
	or [r14], r11
	ret

defcode "xor", _xor, 0
	pspop r11
	xor [r14], r11
	ret

defcode "invert", invert, 0
	not qword [r14]
	ret

defcode "*/mod", starslashmod, 0
	pspop r15 ; n3
	pspop r13 ; n2
	pspop rax ; n1

	imul r13
	idiv r15

	pspush rdx
	pspush rax
	ret
; }}}

; TEMPORARY WONKY DEBUGGING FUNCTIONS {{{
; .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"
; }}}

defcode "HEREDUMP", heredump, 0
	mov rdx, [dp]
	mov r11, [dp0]
	sub rdx, r11
	mov rsi, [dp0]
	mov rdi, 1
	mov rax, __NR_write
	syscall
	ret
; }}}

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