https://github.com/akkartik/mu/blob/master/060read.subx
  1 # read: analogously to write, support reading from in-memory streams in
  2 # addition to file descriptors.
  3 #
  4 # We can pass it either a file descriptor or an address to a stream. If a
  5 # file descriptor is passed in, we _read from it using the right syscall. If a
  6 # stream is passed in (a fake file descriptor), we read from it instead. This
  7 # lets us initialize input for tests.
  8 #
  9 # A little counter-intuitively, the output of 'read' ends up in.. a stream. So
 10 # tests end up doing a redundant copy. Why? Well, consider the alternatives:
 11 #
 12 #   a) Reading into a string, and returning a pointer to the end of the read
 13 #   region, or a count of bytes written. Now this count or end pointer must be
 14 #   managed separately by the caller, which can be error-prone.
 15 #
 16 #   b) Having 'read' return a buffer that it allocates. But there's no way to
 17 #   know in advance how large to make the buffer. If you read less than the
 18 #   size of the buffer you again end up needing to manage initialized vs
 19 #   uninitialized memory.
 20 #
 21 #   c) Creating more helpful variants like 'read-byte' or 'read-until' which
 22 #   also can take a file descriptor or stream, just like 'write'. But such
 23 #   primitives don't exist in the Linux kernel, so we'd be implementing them
 24 #   somehow, either with more internal buffering or by making multiple
 25 #   syscalls.
 26 #
 27 # Reading into a stream avoids these problems. The buffer is externally
 28 # provided and the caller has control over where it's allocated, its lifetime,
 29 # and so on. The buffer's read and write pointers are internal to it so it's
 30 # easier to keep in a consistent state. And it can now be passed directly to
 31 # helpers like 'read-byte' or 'read-until' that only need to support streams,
 32 # never file descriptors.
 33 #
 34 # Like with 'write', we assume our data segment will never begin at an address
 35 # shorter than 0x08000000, so any smaller arguments are assumed to be real
 36 # file descriptors.
 37 #
 38 # As a reminder, a stream looks like this:
 39 #   write: int  # index at which to write to next
 40 #   read: int  # index at which to read next
 41 #   data: (array byte)  # prefixed by length as usual
 42 
 43 == code
 44 #   instruction                     effective address                                                   register    displacement    immediate
 45 # . op          subop               mod             rm32          base        index         scale       r32
 46 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 47 
 48 read:  # f : fd or (addr stream byte), s : (addr stream byte) -> num-bytes-read/eax : int
 49     # . prologue
 50     55/push-ebp
 51     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 52     # if (f < 0x08000000) return _read(f, s)  # f can't be a user-mode address, so treat it as a kernel file descriptor
 53     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(ebp+8)
 54     73/jump-if-greater-unsigned-or-equal  $read:fake/disp8
 55     # . . push args
 56     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 57     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 58     # . . call
 59     e8/call  _read/disp32
 60     # . . discard args
 61     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 62     # return
 63     eb/jump  $read:end/disp8
 64 $read:fake:
 65     # otherwise, treat 'f' as a stream to scan from
 66     # . save registers
 67     56/push-esi
 68     57/push-edi
 69     # esi = f
 70     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
 71     # edi = s
 72     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to esi
 73     # eax = _buffer-4(out = &s->data[s->write], outend = &s->data[s->length],
 74     #                 in  = &f->data[f->read],  inend  = &f->data[f->write])
 75     # . . push &f->data[f->write]
 76     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
 77     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
 78     50/push-eax
 79     # . . push &f->data[f->read]
 80     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
 81     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
 82     50/push-eax
 83     # . . push &s->data[s->length]
 84     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(edi+8) to eax
 85     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
 86     50/push-eax
 87     # . . push &s->data[s->write]
 88     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy *edi to eax
 89     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
 90     50/push-eax
 91     # . . call
 92     e8/call  _buffer-4/disp32
 93     # . . discard args
 94     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 95     # s->write += eax
 96     01/add                          0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # add eax to *edi
 97     # f->read += eax
 98     01/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # add eax to *(esi+4)
 99     # . restore registers
100     5f/pop-to-edi
101     5e/pop-to-esi
102 $read:end:
103     # . epilogue
104     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
105     5d/pop-to-ebp
106     c3/return
107 
108 # - helpers
109 
110 # '_buffer' is like '_append', but silently stops instead of aborting when it runs out of space
111 
112 # 3-argument variant of _buffer
113 _buffer-3:  # out : address, outend : address, s : (array byte) -> num_bytes_buffered/eax
114     # . prologue
115     55/push-ebp
116     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
117     # . save registers
118     51/push-ecx
119     # eax = _buffer-4(out, outend, &s->data[0], &s->data[s->length])
120     # . . push &s->data[s->length]
121     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
122     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
123     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/eax  1/index/ecx   .           1/r32/ecx   4/disp8         .                 # copy eax+ecx+4 to ecx
124     51/push-ecx
125     # . . push &s->data[0]
126     8d/copy-address                 1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   4/disp8         .                 # copy eax+4 to ecx
127     51/push-ecx
128     # . . push outend
129     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
130     # . . push out
131     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
132     # . . call
133     e8/call  _buffer-4/disp32
134     # . . discard args
135     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
136 $_buffer-3:end:
137     # . restore registers
138     59/pop-to-ecx
139     # . epilogue
140     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
141     5d/pop-to-ebp
142     c3/return
143 
144 # 4-argument variant of _buffer
145 _buffer-4:  # out : address, outend : address, in : address, inend : address -> num_bytes_buffered/eax
146     # . prologue
147     55/push-ebp
148     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
149     # . save registers
150     51/push-ecx
151     52/push-edx
152     53/push-ebx
153     56/push-esi
154     57/push-edi
155     # eax/num_bytes_buffered = 0
156     b8/copy-to-eax  0/imm32
157     # edi = out
158     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
159     # edx = outend
160     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
161     # esi = in
162     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0x10/disp8      .                 # copy *(ebp+16) to esi
163     # ecx = inend
164     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
165 $_buffer-4:loop:
166     # if (in >= inend) break
167     39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
168     73/jump-if-greater-or-equal-unsigned  $_buffer-4:end/disp8
169     # if (out >= outend) break  # for now silently ignore filled up buffer
170     39/compare                      3/mod/direct    7/rm32/edi    .           .             .           2/r32/edx   .               .                 # compare edi with edx
171     73/jump-if-greater-or-equal-unsigned  $_buffer-4:end/disp8
172     # *out = *in
173     8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           3/r32/BL    .               .                 # copy byte at *esi to BL
174     88/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           3/r32/BL    .               .                 # copy byte at BL to *edi
175     # ++num_bytes_buffered
176     40/increment-eax
177     # ++in
178     46/increment-esi
179     # ++out
180     47/increment-edi
181     eb/jump  $_buffer-4:loop/disp8
182 $_buffer-4:end:
183     # . restore registers
184     5f/pop-to-edi
185     5e/pop-to-esi
186     5b/pop-to-ebx
187     5a/pop-to-edx
188     59/pop-to-ecx
189     # . epilogue
190     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
191     5d/pop-to-ebp
192     c3/return
193 
194 # idea: a clear-if-empty method on streams that clears only if f->read == f->write
195 # Unclear how I'd use it, though. Callers seem to need the check anyway.
196 # Maybe a better helper would be 'empty-stream?'
197 
198 _read:  # fd : int, s : (addr stream byte) -> num-bytes-read/eax : int
199     # . prologue
200     55/push-ebp
201     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
202     # . save registers
203     51/push-ecx
204     52/push-edx
205     53/push-ebx
206     56/push-esi
207     # esi = s
208     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
209     # eax = s->write
210     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
211     # edx = s->length
212     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
213     # syscall(read, fd, &s->data[s->write], s->length - s->write)
214     # . . fd : ebx
215     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   8/disp8         .                 # copy *(ebp+8) to ebx
216     # . . data : ecx = &s->data[s->write]
217     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           1/r32/ecx   0xc/disp8       .                 # copy esi+eax+12 to ecx
218     # . . size : edx = s->length - s->write
219     29/subtract                     3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # subtract eax from edx
220     # . . syscall
221     b8/copy-to-eax  3/imm32/read
222     cd/syscall  0x80/imm8
223     # add the result eax to s->write
224     01/add                          0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # add eax to *esi
225 $_read:end:
226     # . restore registers
227     5e/pop-to-esi
228     5b/pop-to-ebx
229     5a/pop-to-edx
230     59/pop-to-ecx
231     # . epilogue
232     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
233     5d/pop-to-ebp
234     c3/return
235 
236     # Two options:
237     #   1 (what we have above):
238     #     ecx = s
239     #     eax = s->write
240     #     edx = s->length
241     #     # syscall
242     #     ecx = lea ecx+eax+12
243     #     edx = sub edx eax
244     #
245     #   2:
246     #     ecx = s
247     #     edx = s->length
248     #     ecx = &s->data
249     #     # syscall
250     #     ecx = add ecx, s->write
251     #     edx = sub edx, s->write
252     #
253     # Not much to choose between the two? Option 2 performs a duplicate load to
254     # use one less register, but doesn't increase the amount of spilling (ecx
255     # and edx must be used, and eax must be clobbered anyway).
256 
257 # - tests
258 
259 test-read-single:
260     # - write a single character into _test-stream, then read from it
261     # clear-stream(_test-stream)
262     # . . push args
263     68/push  _test-stream/imm32
264     # . . call
265     e8/call  clear-stream/disp32
266     # . . discard args
267     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
268     # clear-stream(_test-tmp-stream)
269     # . . push args
270     68/push  _test-tmp-stream/imm32
271     # . . call
272     e8/call  clear-stream/disp32
273     # . . discard args
274     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
275     # write(_test-stream, "Ab")
276     # . . push args
277     68/push  "Ab"/imm32
278     68/push  _test-stream/imm32
279     # . . call
280     e8/call  write/disp32
281     # . . discard args
282     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
283     # eax = read(_test-stream, _test-tmp-stream)
284     # . . push args
285     68/push  _test-tmp-stream/imm32
286     68/push  _test-stream/imm32
287     # . . call
288     e8/call  read/disp32
289     # . . discard args
290     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
291     # check-ints-equal(eax, 2, msg)
292     # . . push args
293     68/push  "F - test-read-single: return value"/imm32
294     68/push  2/imm32
295     50/push-eax
296     # . . call
297     e8/call  check-ints-equal/disp32
298     # . . discard args
299     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
300     # check-stream-equal(_test-tmp-stream, "Ab", msg)
301     # . . push args
302     68/push  "F - test-read-single"/imm32
303     68/push  "Ab"/imm32
304     68/push  _test-tmp-stream/imm32
305     # . . call
306     e8/call  check-stream-equal/disp32
307     # . . discard args
308     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
309     # end
310     c3/return
311 
312 test-read-is-stateful:
313     # - make two consecutive reads, check that their results are appended
314     # clear-stream(_test-stream)
315     # . . push args
316     68/push  _test-stream/imm32
317     # . . call
318     e8/call  clear-stream/disp32
319     # . . discard args
320     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
321     # clear-stream(_test-tmp-stream)
322     # . . push args
323     68/push  _test-tmp-stream/imm32
324     # . . call
325     e8/call  clear-stream/disp32
326     # . . discard args
327     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
328     # write(_test-stream, "C")
329     # . . push args
330     68/push  "C"/imm32
331     68/push  _test-stream/imm32
332     # . . call
333     e8/call  write/disp32
334     # . . discard args
335     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
336     # read(_test-stream, _test-tmp-stream)
337     # . . push args
338     68/push  _test-tmp-stream/imm32
339     68/push  _test-stream/imm32
340     # . . call
341     e8/call  read/disp32
342     # . . discard args
343     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
344     # write(_test-stream, "D")
345     # . . push args
346     68/push  "D"/imm32
347     68/push  _test-stream/imm32
348     # . . call
349     e8/call  write/disp32
350     # . . discard args
351     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
352     # read(_test-stream, _test-tmp-stream)
353     # . . push args
354     68/push  _test-tmp-stream/imm32
355     68/push  _test-stream/imm32
356     # . . call
357     e8/call  read/disp32
358     # . . discard args
359     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
360     # check-stream-equal(_test-tmp-stream, "CD", msg)
361     # . . push args
362     68/push  "F - test-read-is-stateful"/imm32
363     68/push  "CD"/imm32
364     68/push  _test-tmp-stream/imm32
365     # . . call
366     e8/call  check-stream-equal/disp32
367     # . . discard args
368     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
369     # end
370     c3/return
371 
372 test-read-returns-0-on-end-of-file:
373     # - read after hitting end-of-file, check that result is 0
374     # setup
375     # . clear-stream(_test-stream)
376     # . . push args
377     68/push  _test-stream/imm32
378     # . . call
379     e8/call  clear-stream/disp32
380     # . . discard args
381     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
382     # . clear-stream(_test-tmp-stream)
383     # . . push args
384     68/push  _test-tmp-stream/imm32
385     # . . call
386     e8/call  clear-stream/disp32
387     # . . discard args
388     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
389     # . write(_test-stream, "Ab")
390     # . . push args
391     68/push  "Ab"/imm32
392     68/push  _test-stream/imm32
393     # . . call
394     e8/call  write/disp32
395     # . . discard args
396     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
397     # first read gets to end-of-file
398     # . read(_test-stream, _test-tmp-stream)
399     # . . push args
400     68/push  _test-tmp-stream/imm32
401     68/push  _test-stream/imm32
402     # . . call
403     e8/call  read/disp32
404     # . . discard args
405     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
406     # second read
407     # . read(_test-stream, _test-tmp-stream)
408     # . . push args
409     68/push  _test-tmp-stream/imm32
410     68/push  _test-stream/imm32
411     # . . call
412     e8/call  read/disp32
413     # . . discard args
414     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
415     # check-ints-equal(eax, 0, msg)
416     # . . push args
417     68/push  "F - test-read-returns-0-on-end-of-file"/imm32
418     68/push  0/imm32
419     50/push-eax
420     # . . call
421     e8/call  check-ints-equal/disp32
422     # . . discard args
423     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
424     # end
425     c3/return
426 
427 == data
428 
429 _test-tmp-stream:  # (stream byte)
430     # current write index
431     0/imm32
432     # current read index
433     0/imm32
434     # length
435     8/imm32
436     # data
437     00 00 00 00 00 00 00 00  # 8 bytes
438 
439 # . . vim:nowrap:textwidth=0