summaryrefslogtreecommitdiff
path: root/eval.asm
blob: 15a9c2a779d8dd468c9620a97bac6f606114729d (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
# Copyright (C) 2026 Sebastian G. Kirmayer <gloria@gloria-mundi.eu>
#
#  This file is part of sysf-i386.
#
#  sysf-i386 is free software: you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option)
#  any later version.
#
#  sysf-i386 is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  details.
#
#  You should have received a copy of the GNU General Public License along
#  with sysf-i386. If not, see <https://www.gnu.org/licenses/>.
#

rodata
align
L s
	long s^
	long $VAL_FIX
	long s^
	long $VAL_ABS
label term_fix

L s
	long s^
	long $VAL_READ
	long s^
	long $VAL_ABS
	long s^
	long $VAL_ABS
	long s^
	long $VAL_ABS
label term_read

L s
	long s^
	long $VAL_WRITE0
	long s^
	long $VAL_ABS
label term_write0

L s
	long s^
	long $VAL_WRITE1
	long s^
	long $VAL_ABS
label term_write1

L s
	long s^
	long $VAL_EXIT
label term_exit

bss
align
label alloc_next
	long 0

data
align
label context_root
	long 0
	long 0
	long 0
	long 0x10

# Context elements:
#   0: parent
#   4: context
#   8: term
#  12: refcount

text
label execute
	pushl 0
	pushl term_exit
	pushl context_root
	pushl 0
	pushl term_write1
	pushl context_root
	pushl 0
	pushl term_write0
	pushl context_root
	pushl 0
	pushl term_read
	pushl context_root
	pushl 0
	pushl term_fix
	pushl context_root
	pushl 0
	pushl %eax
	pushl context_root

L exec_loop
	movl @%esp+4 %eax
	movl @%eax+-4 %ebx
	testl %ebx %ebx
	js special.

L var
	movl @%esp %esi
L loop
	decl %ebx %ebx
	js done.
	movl @%esi %esi
	jmp loop^
L done
	# %esi -- variable context
	movl @%esi+8 %edi
	# %edi -- new term
	cmpl $VAL_ABS @%edi+-4
	je no_writeback.
	movl @%esp+8 %ebx
	# %ebx -- old writeback
	testl %ebx %ebx
	jz no_old_writeback.
	# write current value back
	movl @%ebx+4 %eax
	call unref
	movl @%esp %ecx
	incl @%ecx+12
	movl %ecx @%ebx+4
	movl @%esp+4 %ecx
	movl %ecx @%ebx+8
	movl %ebx %eax
	call unref
L no_old_writeback
	incl @%esi+12
	movl %esi @%esp+8
L no_writeback
	movl %edi @%esp+4
	movl @%esi+4 %eax
	incl @%eax+12
	xchgl %eax @%esp
	call unref
	jmp exec_loop^

L special
	cmpl $VAL_APP %ebx
	je app.
	cmpl $VAL_ABS %ebx
	je abs.
	cmpl $VAL_FIX %ebx
	je fix.
	cmpl $VAL_READ %ebx
	je read.
	cmpl $VAL_WRITE0 %ebx
	je write0.
	cmpl $VAL_WRITE1 %ebx
	je write1.
	cmpl $VAL_EXIT %ebx
	je exit
	ud2

L app
	pushl 0
	subl 8 %eax
	movl %eax @%esp+8
	pushl @%eax+-8
	movl @%esp+8 %eax
	incl @%eax+12
	pushl %eax
	jmp exec_loop^

L abs
	popl %ebx
	popl %ebp
	popl %edi
	# %ebx -- context
	# %ebp -- term
	# %edi -- writeback
	testl %edi %edi
	jz no_writeback.
	movl @%edi+4 %eax
	call unref
	incl @%ebx+12
	movl %ebx @%edi+4
	movl %ebp @%edi+8
	movl %edi %eax
	call unref
L no_writeback
	movl @alloc_next %eax
	testl %eax %eax
	jz no_next.
	movl @%eax %ecx
	movl %ecx @alloc_next
L have_ctx
	movl %ebx @%eax
	movl @%esp %ecx
	movl %ecx @%eax+4
	movl @%esp+4 %ecx
	movl %ecx @%eax+8
	movl 1 @%eax+12
	movl %eax @%esp
	subl 8 %ebp
	movl %ebp @%esp+4
	jmp exec_loop^

L no_next
	movl @heap_ptr %esi
	movl %esi %eax
	addl 16 %esi
	movl %esi @heap_ptr
	cmpl @heap_end %esi
	ja realloc.
	jmp have_ctx^
L realloc
	movl %eax %esi
	call alloc
	movl %esi %eax
	jmp have_ctx^

L fix
	movl @%esp %eax
	incl @%eax+12
	pushl %eax
	pushl @%eax+8
	movl @%eax+4 %eax
	incl @%eax+12
	pushl %eax
	jmp exec_loop^

L read
	call read
	movl 1 %ebx
	subl %eax %ebx
	jmp var^

L write0
	call write0
	xorl %ebx %ebx
	jmp var^

L write1
	call write1
	xorl %ebx %ebx
	jmp var^

label unref
	decl @%eax+12
	jz free.
	ret
L free
	pushl %eax
	movl @%eax %eax
	call unref
	movl @%esp %eax
	movl @%eax+4 %eax
	call unref
	popl %eax
	movl @alloc_next %ecx
	movl %eax @alloc_next
	movl %ecx @%eax
	ret