summaryrefslogtreecommitdiff
path: root/sanctuary.s
blob: 235f384608e4decdc7871d50c2ad3548ab02b168 (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
; sanctuary

; macros {{{
; TODO: error handling (once i add that)
%macro pspush 1
	lea r14, [r14-8]
	mov qword [r14], r15
	mov r15, %1
%endmacro

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

%macro psdrop 0
	lea r14, [r14+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 INTERPRET 0x0
%assign COMPILING (~0x1)

%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 r14, [wstk + 8]

	call brk@
	mov qword [dp], r15
	mov qword [dp0], r15
	mov r15, 0x9c400
	call grow
	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 rdi ; addr
	pspop rsi
	mov rax, __NR_mprotect
	syscall
	ret

defcode "here", here, 0
	pspush qword [dp]
	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

	; is there a better way of checking before?
	cmp rsi, r10
	jge .wordloop_e
	inc r11
	lodsb
	je .wordloop

.wordloop_e:
	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
	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, r15b
	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, r15b
	je .wordloop_e
	cmp al, 0x0a
	je .wordloop_e

	; is there a better way of checking before?
	cmp rsi, r10
	jge .wordloop_e
	inc r11
	lodsb
	je .wordloop

.wordloop_e:
	sub rsi, qword [tib]
	mov qword [to_in], rsi
	pop rsi
	pspop r8
	pspush rsi
	pspush r11
	ret

.empty:
	pspop r8
	pspush 0
	pspush 0
	ret
; }}}

defvar "state", state, 0, INTERPRET
defvar "dp", dp, 0, 0
defvar "dp0", dp0, 0, 0
defvar "dp$", dp$, 0, 0
defvar "tib", tib, 0, 0 ; todo set correct initial value
defvar "#tib", n_tib, 0, 0 ; todo set correct initial value
defvar ">in", to_in, 0, 0
defvar "latest", latest, 0, lfa_latest