# read-byte-buffered: one higher-level abstraction atop 'read'. # # There are many situations where 'read' is a lot to manage, and we need # to abstract some details away. One of them is when we want to read a file # character by character. In this situation we follow C's FILE data structure, # which manages the underlying file descriptor together with the buffer it # reads into. We call our version 'buffered-file'. Should be useful with other # primitives as well, in later layers. == data # The buffered file for standard input. Also illustrates the layout for # buffered-file: a pointer to the backing store, followed by a 'buffer' stream Stdin: # buffered-file # file descriptor or (addr stream byte) 0/imm32 # standard input $Stdin->buffer: # inlined fields for a stream # current write index 0/imm32 # current read index 0/imm32 # size 8/imm32 # data 00 00 00 00 00 00 00 00 # 8 bytes # TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But # I don't want to type in 1024 bytes here. == code # instruction effective address register displacement immediate # . op subop mod rm32 base index scale r32 # . 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 # Return next byte value in eax, with top 3 bytes cleared. # On reaching end of file, return 0xffffffff (Eof). read-byte-buffered: # f: (addr buffered-file) -> byte-or-Eof/eax: byte # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # . save registers 51/push-ecx 56/push-esi # esi = f 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # ecx = f->read 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 8/disp8 . # copy *(esi+8) to ecx # if (f->read >= f->write) populate stream from file 3b/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # compare ecx with *(esi+4) 7c/jump-if-< $read-byte-buffered:from-stream/disp8 # . clear-stream(stream = f+4) # . . push args 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy esi+4 to eax 50/push-eax # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . f->read must now be 0; update its cache at ecx 31/xor 3/mod/direct 1/rm32/ecx . . . 1/r32/ecx . . # clear ecx # . eax = read(f->fd, stream = f+4) # . . push args 50/push-eax ff 6/subop/push 0/mod/indirect 6/rm32/esi . . . . . . # push *esi # . . call e8/call read/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (eax == 0) return 0xffffffff 3d/compare-eax-and 0/imm32 75/jump-if-!= $read-byte-buffered:from-stream/disp8 b8/copy-to-eax 0xffffffff/imm32/Eof eb/jump $read-byte-buffered:end/disp8 $read-byte-buffered:from-stream: # byte-or-Eof = f->data[f->read] 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0x10/disp8 . # copy byte at *(esi+ecx+16) to AL # ++f->read ff 0/subop/increment 1/mod/*+disp8 6/rm32/esi . . . . 8/disp8 . # increment *(esi+8) $read-byte-buffered:end: # . restore registers 5e/pop-to-esi 59/pop-to-ecx # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp c3/return # - tests test-read-byte-buffered-single: # - check that read-byte-buffered returns first byte of 'file' # setup # . clear-stream(_test-stream) # . . push args 68/push _test-stream/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . clear-stream(_test-buffered-file->buffer) # . . push args 68/push $_test-buffered-file->buffer/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . write(_test-stream, "Ab") # . . push args 68/push "Ab"/imm32 68/push _test-stream/imm32 # . . call e8/call write/disp32
# Create a new segment (pool of memory for allocating chunks from) in the form
# of an *allocation descriptor* that can be passed to the memory allocator
# (defined in a later layer).
#
# Currently an allocation descriptor consists of just the bounds of the pool of
# available memory:
#
# curr: address
# end: address
#
# This isn't enough information to reclaim individual allocations. We can't
# support arbitrary reclamation yet.
== code
# instruction effective address register displacement immediate
# . op subop mod rm32 base index scale r32
# . 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
Entry: # manual test
# var ad/ecx: allocation-descriptor
68/push 0/imm32/limit
68/push 0/imm32/curr
89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx
# new-segment(0x1000, ad)
# . . push args
51/push-ecx
68/push 0x1000/imm32
# . . call
e8/call new-segment/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# var eax: (addr _) = ad->curr
8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax
# write to *eax to check that we have access to the newly-allocated segment
c7 0/subop/copy 0/mod/direct 0/rm32/eax . . . . . 0x34/imm32 # copy to *eax
# syscall(exit, eax)
89/copy 3/mod/direct 3/rm32/ebx . . . 0/r32/eax . . # copy eax to ebx
e8/call syscall_exit/disp32
new-segment: # len: int, ad: (addr allocation-descriptor)
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
50/push-eax
53/push-ebx
# copy len to _mmap-new-segment->len
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # copy *(ebp+8) to eax
89/copy 0/mod/indirect 5/rm32/.disp32 . . 0/r32/eax $_mmap-new-segment:len/disp32 # copy eax to *$_mmap-new-segment:len
# mmap(_mmap-new-segment)
bb/copy-to-ebx _mmap-new-segment/imm32
e8/call syscall_mmap/disp32
# copy {eax, eax+len} to *ad
# . ebx = ad
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 3/r32/ebx 0xc/disp8 . # copy *(ebp+12) to ebx
# . ad->curr = eax
89/copy 0/mod/indirect 3/rm32/ebx . . . 0/r32/eax . . # copy eax to *ebx
# . ad->end = eax+len
03/add 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # add *(ebp+8) to eax
89/copy 1/mod/*+disp8 3/rm32/ebx . . . 0/r32/eax 4/disp8 . # copy eax to *(ebx+4)
$new-segment:end:
# . restore registers
5b/pop-to-ebx
58/pop-to-eax
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
== data
# various constants used here were found in the Linux sources (search for file mman-common.h)
_mmap-new-segment: # mmap_arg_struct
# addr
0/imm32
$_mmap-new-segment:len:
# len
0/imm32
# protection flags
3/imm32 # PROT_READ | PROT_WRITE
# sharing flags
0x22/imm32 # MAP_PRIVATE | MAP_ANONYMOUS
# fd
-1/imm32 # since MAP_ANONYMOUS is specified
# offset
0/imm32 # since MAP_ANONYMOUS is specified
# . . vim:nowrap:textwidth=0