https://github.com/akkartik/mu/blob/master/subx/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 (address stream), s : (address stream) -> num-bytes-read/EAX
     49     # . prolog
     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     7d/jump-if-greater-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     # . epilog
    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     # . prolog
    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     # . epilog
    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     # . prolog
    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     7d/jump-if-greater-or-equal  $_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     7d/jump-if-greater-or-equal  $_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     # . epilog
    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
    195 # idea: a clear-if-empty method on streams that clears only if f->read == f->write
    196 # Unclear how I'd use it, though. Callers seem to need the check anyway.
    197 # Maybe a better helper would be 'empty-stream?'
    198
    199 _read:  # fd : int, s : (address stream) -> num-bytes-read/EAX
    200     # . prolog
    201     55/push-EBP
    202     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
    203     # . save registers
    204     51/push-ECX
    205     52/push-EDX
    206     53/push-EBX
    207     56/push-ESI
    208     # ESI = s
    209     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
    210     # EAX = s->write
    211     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy *ESI to EAX
    212     # EDX = s->length
    213     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           2/r32/EDX   8/disp8         .                 # copy *(ESI+8) to EDX
    214     # syscall(read, fd, &s->data[s->write], s->length - s->write)
    215     # . . fd : EBX
    216     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   8/disp8         .                 # copy *(EBP+8) to EBX
    217     # . . data : ECX = &s->data[s->write]
    218     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
    219     # . . size : EDX = s->length - s->write
    220     29/subtract                     3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # subtract EAX from EDX
    221     # . . syscall
    222     b8/copy-to-EAX  3/imm32/read
    223     cd/syscall  0x80/imm8
    224     # add the result EAX to s->write
    225     01/add                          0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # add EAX to *ESI
    226 $_read:end:
    227     # . restore registers
    228     5e/pop-to-ESI
    229     5b/pop-to-EBX
    230     5a/pop-to-EDX
    231     59/pop-to-ECX
    232     # . epilog
    233     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
    234     5d/pop-to-EBP
    235     c3/return
    236
    237     # Two options:
    238     #   1 (what we have above):
    239     #     ECX = s
    240     #     EAX = s->write
    241     #     EDX = s->length
    242     #     # syscall
    243     #     ECX = lea ECX+EAX+12
    244     #     EDX = sub EDX EAX
    245     #
    246     #   2:
    247     #     ECX = s
    248     #     EDX = s->length
    249     #     ECX = &s->data
    250     #     # syscall
    251     #     ECX = add ECX, s->write
    252     #     EDX = sub EDX, s->write
    253     #
    254     # Not much to choose between the two? Option 2 performs a duplicate load to
    255     # use one less register, but doesn't increase the amount of spilling (ECX
    256     # and EDX must be used, and EAX must be clobbered anyway).
    257
    258 # - tests
    259
    260 test-read-single:
    261     # - write a single character into _test-stream, then read from it
    262     # clear-stream(_test-stream)
    263     # . . push args
    264     68/push  _test-stream/imm32
    265     # . . call
    266     e8/call  clear-stream/disp32
    267     # . . discard args
    268     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    269     # clear-stream(_test-tmp-stream)
    270     # . . push args
    271     68/push  _test-tmp-stream/imm32
    272     # . . call
    273     e8/call  clear-stream/disp32
    274     # . . discard args
    275     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    276     # write(_test-stream, "Ab")
    277     # . . push args
    278     68/push  "Ab"/imm32
    279     68/push  _test-stream/imm32
    280     # . . call
    281     e8/call  write/disp32
    282     # . . discard args
    283     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    284     # EAX = read(_test-stream, _test-tmp-stream)
    285     # . . push args
    286     68/push  _test-tmp-stream/imm32
    287     68/push  _test-stream/imm32
    288     # . . call
    289     e8/call  read/disp32
    290     # . . discard args
    291     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    292     # check-ints-equal(EAX, 2, msg)
    293     # . . push args
    294     68/push  "F - test-read-single: return EAX"/imm32
    295     68/push  2/imm32
    296     50/push-EAX
    297     # . . call
    298     e8/call  check-ints-equal/disp32
    299     # . . discard args
    300     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    301     # check-stream-equal(_test-tmp-stream, "Ab", msg)
    302     # . . push args
    303     68/push  "F - test-read-single"/imm32
    304     68/push  "Ab"/imm32
    305     68/push  _test-tmp-stream/imm32
    306     # . . call
    307     e8/call  check-stream-equal/disp32
    308     # . . discard args
    309     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    310     # end
    311     c3/return
    312
    313 test-read-is-stateful:
    314     # - make two consecutive reads, check that their results are appended
    315     # clear-stream(_test-stream)
    316     # . . push args
    317     68/push  _test-stream/imm32
    318     # . . call
    319     e8/call  clear-stream/disp32
    320     # . . discard args
    321     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    322     # clear-stream(_test-tmp-stream)
    323     # . . push args
    324     68/push  _test-tmp-stream/imm32
    325     # . . call
    326     e8/call  clear-stream/disp32
    327     # . . discard args
    328     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    329     # write(_test-stream, "C")
    330     # . . push args
    331     68/push  "C"/imm32
    332     68/push  _test-stream/imm32
    333     # . . call
    334     e8/call  write/disp32
    335     # . . discard args
    336     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    337     # read(_test-stream, _test-tmp-stream)
    338     # . . push args
    339     68/push  _test-tmp-stream/imm32
    340     68/push  _test-stream/imm32
    341     # . . call
    342     e8/call  read/disp32
    343     # . . discard args
    344     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    345     # write(_test-stream, "D")
    346     # . . push args
    347     68/push  "D"/imm32
    348     68/push  _test-stream/imm32
    349     # . . call
    350     e8/call  write/disp32
    351     # . . discard args
    352     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    353     # read(_test-stream, _test-tmp-stream)
    354     # . . push args
    355     68/push  _test-tmp-stream/imm32
    356     68/push  _test-stream/imm32
    357     # . . call
    358     e8/call  read/disp32
    359     # . . discard args
    360     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    361     # check-stream-equal(_test-tmp-stream, "CD", msg)
    362     # . . push args
    363     68/push  "F - test-read-is-stateful"/imm32
    364     68/push  "CD"/imm32
    365     68/push  _test-tmp-stream/imm32
    366     # . . call
    367     e8/call  check-stream-equal/disp32
    368     # . . discard args
    369     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    370     # end
    371     c3/return
    372
    373 test-read-returns-0-on-end-of-file:
    374     # - read after hitting end-of-file, check that result is 0
    375     # setup
    376     # . clear-stream(_test-stream)
    377     # . . push args
    378     68/push  _test-stream/imm32
    379     # . . call
    380     e8/call  clear-stream/disp32
    381     # . . discard args
    382     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    383     # . clear-stream(_test-tmp-stream)
    384     # . . push args
    385     68/push  _test-tmp-stream/imm32
    386     # . . call
    387     e8/call  clear-stream/disp32
    388     # . . discard args
    389     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    390     # . write(_test-stream, "Ab")
    391     # . . push args
    392     68/push  "Ab"/imm32
    393     68/push  _test-stream/imm32
    394     # . . call
    395     e8/call  write/disp32
    396     # . . discard args
    397     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    398     # first read gets to end-of-file
    399     # . read(_test-stream, _test-tmp-stream)
    400     # . . push args
    401     68/push  _test-tmp-stream/imm32
    402     68/push  _test-stream/imm32
    403     # . . call
    404     e8/call  read/disp32
    405     # . . discard args
    406     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    407     # second read
    408     # . read(_test-stream, _test-tmp-stream)
    409     # . . push args
    410     68/push  _test-tmp-stream/imm32
    411     68/push  _test-stream/imm32
    412     # . . call
    413     e8/call  read/disp32
    414     # . . discard args
    415     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    416     # check-ints-equal(EAX, 0, msg)
    417     # . . push args
    418     68/push  "F - test-read-returns-0-on-end-of-file"/imm32
    419     68/push  0/imm32
    420     50/push-EAX
    421     # . . call
    422     e8/call  check-ints-equal/disp32
    423     # . . discard args
    424     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    425     # end
    426     c3/return
    427
    428 == data
    429
    430 _test-tmp-stream:
    431     # current write index
    432     0/imm32
    433     # current read index
    434     0/imm32
    435     # length
    436     8/imm32
    437     # data
    438     00 00 00 00 00 00 00 00  # 8 bytes
    439
    440 # . . vim:nowrap:textwidth=0