c{0: 0 (((1 integer)) <- ((copy)) ((2 literal))) -- nil
c{0: 1 (((2 integer)) <- ((copy)) ((23 literal))) -- nil
c{0: 2 (((3 boolean)) <- ((copy)) ((nil literal))) -- nil
c{0: 3 (((4 integer)) <- ((copy)) ((24 literal))) -- nil
c{0: 4 (((5 boolean)) <- ((copy)) ((t literal))) -- nil
c{0: 5 (((6 integer)) <- ((copy)) ((1 literal))) -- nil
c{0: 6 (((7 integer-boolean-pair-array-address)) <- ((copy)) ((1 literal))) -- nil
c{0: 7 (((8 integer-boolean-pair)) <- ((index)) ((7 integer-boolean-pair-array-address) (deref)) ((6 integer))) -- nil
c{1: 0 ✓ (((1 integer)) <- ((copy)) ((2 literal)))
c{1: 1 ✓ (((2 integer)) <- ((copy)) ((23 literal)))
c{1: 2 ✓ (((3 boolean)) <- ((copy)) ((nil literal)))
c{1: 3 ✓ (((4 integer)) <- ((copy)) ((24 literal)))
c{1: 4 ✓ (((5 boolean)) <- ((copy)) ((t literal)))
c{1: 5 ✓ (((6 integer)) <- ((copy)) ((1 literal)))
c{1: 6 ✓ (((7 integer-boolean-pair-array-address)) <- ((copy)) ((1 literal)))
c{1: 7 ✓ (((8 integer-boolean-pair)) <- ((index)) ((7 integer-boolean-pair-array-address) (deref)) ((6 integer)))
cn0: convert-names in main
cn0: (((1 integer)) <- ((copy)) ((2 literal))) nil nil
cn0: checking arg ((2 literal))
cn0: checking oarg ((1 integer))
maybe-add: ((1 integer))
cn0: (((2 integer)) <- ((copy)) ((23 literal))) nil nil
cn0: checking arg ((23 literal))
cn0: checking oarg ((2 integer))
maybe-add: ((2 integer))
cn0: (((3 boolean)) <- ((copy)) ((nil literal))) nil nil
cn0: checking arg ((nil literal))
cn0: checking oarg ((3 boolean))
maybe-add: ((3 boolean))
cn0: (((4 integer)) <- ((copy)) ((24 literal))) nil nil
cn0: checking arg ((24 literal))
cn0: checking oarg ((4 integer))
maybe-add: ((4 integer))
cn0: (((5 boolean)) <- ((copy)) ((t literal))) nil nil
cn0: checking arg ((t literal))
cn0: checking oarg ((5 boolean))
maybe-add: ((5 boolean))
cn0: (((6 integer)) <- ((copy)) ((1 literal))) nil nil
cn0: checking arg ((1 literal))
cn0: checking oarg ((6 integer))
maybe-add: ((6 integer))
cn0: (((7 integer-boolean-pair-array-address)) <- ((copy)) ((1 literal))) nil nil
cn0: checking arg ((1 literal))
cn0: checking oarg ((7 integer-boolean-pair-array-address))
maybe-add: ((7 integer-boolean-pair-array-address))
cn0: (((8 integer-boolean-pair)) <- ((index)) ((7 integer-boolean-pair-array-address) (deref)) ((6 integer))) nil nil
cn0: checking arg ((7 integer-boolean-pair-array-address) (deref))
maybe-add: ((7 integer-boolean-pair-array-address) (deref))
cn0: checking arg ((6 integer))
maybe-add: ((6 integer))
cn0: checking oarg ((8 integer-boolean-pair))
maybe-add: ((8 integer-boolean-pair))
cn1: (((1 integer)) <- ((copy)) ((2 literal)))
cn1: (((2 integer)) <- ((copy)) ((23 literal)))
cn1: (((3 boolean)) <- ((copy)) ((nil literal)))
cn1: (((4 integer)) <- ((copy)) ((24 literal)))
cn1: (((5 boolean)) <- ((copy)) ((t literal)))
cn1: (((6 integer)) <- ((copy)) ((1 literal)))
cn1: (((7 integer-boolean-pair-array-address)) <- ((copy)) ((1 literal)))
cn1: (((8 integer-boolean-pair)) <- ((index)) ((7 integer-boolean-pair-array-address) (deref)) ((6 integer)))
schedule: main
run: main 0: (((1 integer)) <- ((copy)) ((2 literal)))
run: main 0: 2 => ((1 integer))
mem: ((1 integer)): 1 <= 2
run: main 1: (((2 integer)) <- ((copy)) ((23 literal)))
run: main 1: 23 => ((2 integer))
mem: ((2 integer)): 2 <= 23
run: main 2: (((3 boolean)) <- ((copy)) ((nil literal)))
run: main 2: nil => ((3 boolean))
mem: ((3 boolean)): 3 <= nil
run: main 3: (((4 integer)) <- ((copy)) ((24 literal)))
run: main 3: 24 => ((4 integer))
mem: ((4 integer)): 4 <= 24
run: main 4: (((5 boolean)) <- ((copy)) ((t literal)))
run: main 4: t => ((5 boolean))
mem: ((5 boolean)): 5 <= t
run: main 5: (((6 integer)) <- ((copy)) ((1 literal)))
run: main 5: 1 => ((6 integer))
mem: ((6 integer)): 6 <= 1
run: main 6: (((7 integer-boolean-pair-array-address)) <- ((copy)) ((1 literal)))
run: main 6: 1 => ((7 integer-boolean-pair-array-address))
mem: ((7 integer-boolean-pair-array-address)): 7 <= 1
run: main 7: (((8 integer-boolean-pair)) <- ((index)) ((7 integer-boolean-pair-array-address) (deref)) ((6 integer)))
mem: ((6 integer)) => 1
array-len: ((1 integer-boolean-pair-array))
mem: ((1 integer)) => 2
mem: ((4 integer-boolean-pair) (raw)) => #(tagged record (24 t . nil))
run: main 7: #(tagged record (24 t . nil)) => ((8 integer-boolean-pair))
mem: ((8 integer-boolean-pair)): 8 <= 24
mem: ((8 integer-boolean-pair)): 9 <= t
schedule: done with routine nil
ht: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */# 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.
Stdin:
# file descriptor or (address stream)
0/imm32 # standard input
# current write index
0/imm32
# current read index
0/imm32
# length
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 : (address buffered-file) -> byte-or-Eof/eax
# . prolog
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-lesser $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-not-equal $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:
# read byte from stream
# AL = 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
# . epilog
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+4)
# . . push args
b8/copy-to-eax _test-buffered-file/imm32
05/add-to-eax 4/imm32
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
# . write(_test-stream, "Ab")
# . . push args
68/push "Ab"/imm32
68/push _test-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# read-byte-buffered(_test-buffered-file)
# . . push args
68/push _test-buffered-file/imm32
# . . call
e8/call read-byte-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 'A', msg)
# . . push args
68/push "F - test-read-byte-buffered-single"/imm32
68/push 0x41/imm32
50/push-eax
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
# . end
c3/return
test-read-byte-buffered-multiple:
# - call read-byte-buffered twice, check that second call returns second byte
# 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+4)
# . . push args
b8/copy-to-eax _test-buffered-file/imm32
05/add-to-eax 4/imm32
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
# . write(_test-stream, "Ab")
# . . push args
68/push "Ab"/imm32
68/push _test-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# read-byte-buffered(_test-buffered-file)
# . . push args
68/push _test-buffered-file/imm32
# . . call
e8/call read-byte-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# read-byte-buffered(_test-buffered-file)
# . . push args
68/push _test-buffered-file/imm32
# . . call
e8/call read-byte-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 'b', msg)
# . . push args
68/push "F - test-read-byte-buffered-multiple"/imm32
68/push 0x62/imm32
50/push-eax
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
# . end
c3/return
test-read-byte-buffered-end-of-file:
# - call read-byte-buffered on an empty 'file', check that it returns Eof
# 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+4)
# . . push args
b8/copy-to-eax _test-buffered-file/imm32
05/add-to-eax 4/imm32
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
# read-byte-buffered(_test-buffered-file)
# . . push args
68/push _test-buffered-file/imm32
# . . call
e8/call read-byte-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 0xffffffff, msg)
# . . push args
68/push "F - test-read-byte-buffered-end-of-file"/imm32
68/push 0xffffffff/imm32/Eof
50/push-eax
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
# . end
c3/return
test-read-byte-buffered-refills-buffer:
# - consume buffered-file's buffer, check that next read-byte-buffered still works
# 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+4)
# . . push args
b8/copy-to-eax _test-buffered-file/imm32
05/add-to-eax 4/imm32
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
# . write(_test-stream, "Abcdefgh")
# . . push args
68/push "Abcdefgh"/imm32
68/push _test-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# pretend buffer is full
# . _test-buffered-file->read = 6 # >= _test-buffered-file->length
b8/copy-to-eax _test-buffered-file/imm32
c7 0/subop/copy 1/mod/*+disp8 0/rm32/eax . . . . 8/disp8 6/imm32 # copy to *(eax+8)
# read-byte-buffered(_test-buffered-file)
# . . push args
68/push _test-buffered-file/imm32
# . . call
e8/call read-byte-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 'A', msg)
# . . push args
68/push "F - test-read-byte-buffered-refills-buffer"/imm32
68/push 0x41/imm32
50/push-eax
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
# . end
c3/return
== data
# a test buffered file for _test-stream
_test-buffered-file:
# file descriptor or (address stream)
_test-stream/imm32
# current write index
0/imm32
# current read index
0/imm32
# length
6/imm32
# data
00 00 00 00 00 00 # 6 bytes
_test-input-stream:
# current write index
0/imm32
# current read index
0/imm32
# length
0x100/imm32 # 256 bytes
# data (16 lines x 16 bytes/line)
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# a test buffered file for _test-input-stream
_test-input-buffered-file:
# file descriptor or (address stream)
_test-input-stream/imm32
# current write index
0/imm32
# current read index
0/imm32
# length
6/imm32
# data
00 00 00 00 00 00 # 6 bytes
# . . vim:nowrap:textwidth=0