1 # Some unsafe methods not intended to be used directly in SubX, only through
2 # Mu after proper type-checking.
6 stream-empty?: # s: (addr stream _) -> result/eax: boolean
14 b8/copy-to-eax 0/imm32/false
16 8b/-> *(ebp+8) 6/r32/esi
17 # return s->read >= s->write
19 39/compare-with *(esi+4) 1/r32/ecx
30 stream-full?: # s: (addr stream _) -> result/eax: boolean
38 b8/copy-to-eax 0/imm32/false
40 8b/-> *(ebp+8) 6/r32/esi
41 # return s->write >= s->size
42 8b/-> *(esi+8) 1/r32/ecx
43 39/compare-with *esi 1/r32/ecx
54 write-to-stream: # s: (addr stream _), in: (addr byte), n: int
65 8b/-> *(ebp+8) 7/r32/edi
66 # var swrite/edx: int = s->write
68 # if (swrite + n > s->size) abort
69 8b/-> *(ebp+0x10) 1/r32/ecx
70 01/add-to %ecx 2/r32/edx
71 3b/compare 1/r32/ecx *(edi+8)
72 0f 8f/jump-if-> $write-to-stream:abort/disp32
73 # var out/edx: (addr byte) = s->data + s->write
74 8d/copy-address *(edi+edx+0xc) 2/r32/edx
75 # var outend/ebx: (addr byte) = out + n
76 8b/-> *(ebp+0x10) 3/r32/ebx
77 8d/copy-address *(edx+ebx) 3/r32/ebx
79 8b/-> *(ebp+0xc) 0/r32/eax
80 # var inend/ecx: (addr byte) = in + n
81 8b/-> *(ebp+0x10) 1/r32/ecx
82 8d/copy-address *(eax+ecx) 1/r32/ecx
84 (_append-4 %edx %ebx %eax %ecx) # => eax
86 8b/-> *(ebp+0x10) 1/r32/ecx
87 01/add-to *edi 1/r32/ecx
100 $write-to-stream:abort:
101 (abort "write-to-stream: stream full")
104 read-from-stream: # s: (addr stream _), out: (addr byte), n: int
115 8b/-> *(ebp+8) 6/r32/esi
116 # var sread/edx: int = s->read
117 8b/-> *(esi+4) 2/r32/edx
118 # if (sread + n > s->write) abort
119 8b/-> *(ebp+0x10) 1/r32/ecx
120 01/add-to %ecx 2/r32/edx
121 3b/compare 1/r32/ecx *esi
122 0f 8f/jump-if-> $read-from-stream:abort/disp32
123 # var in/edx: (addr byte) = s->data + s->read
124 8d/copy-address *(esi+edx+0xc) 2/r32/edx
125 # var inend/ebx: (addr byte) = in + n
126 8b/-> *(ebp+0x10) 3/r32/ebx
127 8d/copy-address *(edx+ebx) 3/r32/ebx
129 8b/-> *(ebp+0xc) 0/r32/eax
130 # var outend/ecx: (addr byte) = out + n
131 8b/-> *(ebp+0x10) 1/r32/ecx
132 8d/copy-address *(eax+ecx) 1/r32/ecx
134 (_append-4 %eax %ecx %edx %ebx) # => eax
136 8b/-> *(ebp+0x10) 1/r32/ecx
137 01/add-to *(esi+4) 1/r32/ecx
138 $read-from-stream:end:
139 # . restore registers
150 $read-from-stream:abort:
151 (abort "read-from-stream: stream empty")
154 stream-first: # s: (addr stream byte) -> result/eax: byte
162 b8/copy-to-eax 0/imm32
164 8b/-> *(ebp+8) 6/r32/esi
165 # var idx/ecx: int = s->read
166 8b/-> *(esi+4) 1/r32/ecx
167 # if idx >= s->write return 0
168 3b/compare-with 1/r32/ecx *esi
169 7d/jump-if->= $stream-first:end/disp8
170 # result = s->data[idx]
171 8a/byte-> *(esi+ecx+0xc) 0/r32/AL
173 # . restore registers
181 stream-final: # s: (addr stream byte) -> result/eax: byte
189 b8/copy-to-eax 0/imm32
191 8b/-> *(ebp+8) 6/r32/esi
192 # var max/ecx: int = s->write
194 # if s->read >= max return 0
195 39/compare-with *(esi+4) 1/r32/ecx
196 7d/jump-if->= $stream-final:end/disp8
197 # var idx/ecx: int = max - 1
199 # result = s->data[idx]
200 8a/byte-> *(esi+ecx+0xc) 0/r32/AL
202 # . restore registers
210 # compare all the data in two streams (ignoring the read pointer)
211 streams-data-equal?: # a: (addr stream byte), b: (addr array byte) -> result/eax: boolean
214 # if (awrite != b->write) return false
221 # if (c1 != c2) return false
222 # i+=4, curra+=4, currb+=4
243 8b/-> *(ebp+8) 6/r32/esi
245 8b/-> *(ebp+0xc) 7/r32/edi
246 # var awrite/edx: int = a->write
248 $streams-data-equal?:sizes:
249 # if (awrite != b->write) return false
250 39/compare *edi 2/r32/edx
251 75/jump-if-!= $streams-data-equal?:false/disp8
252 # var curra/esi: (addr byte) = a->data
253 81 0/subop/add %esi 0xc/imm32
254 # var currb/edi: (addr byte) = b->data
255 81 0/subop/add %edi 0xc/imm32
257 31/xor-with %ecx 1/r32/ecx
259 31/xor-with %eax 0/r32/eax
261 31/xor-with %ebx 3/r32/ebx
262 $streams-data-equal?:loop:
264 # if (i >= awrite) return true
265 39/compare %ecx 2/r32/edx
266 7d/jump-if->= $streams-data-equal?:true/disp8
267 # var vala/eax: int = *curra
268 8a/byte-> *esi 0/r32/eax
269 # var valb/ebx: int = *currb
270 8a/byte-> *edi 3/r32/ebx
271 # if (vala != valb) return false
272 39/compare %eax 3/r32/ebx
273 75/jump-if-!= $streams-data-equal?:false/disp8
282 $streams-data-equal?:true:
283 b8/copy-to-eax 1/imm32
284 eb/jump $streams-data-equal?:end/disp8
285 $streams-data-equal?:false:
286 b8/copy-to-eax 0/imm32
287 $streams-data-equal?:end:
288 # . restore registers
300 check-streams-data-equal: # s: (addr stream _), expected: (addr array _), msg: (addr array byte)
307 (streams-data-equal? *(ebp+8) *(ebp+0xc)) # => eax
308 (check-ints-equal %eax 1 *(ebp+0x10))
309 $check-streams-data-equal:end:
310 # . restore registers