diff options
Diffstat (limited to 'subx')
-rw-r--r-- | subx/061read-byte.subx | 62 | ||||
-rw-r--r-- | subx/064write-byte.subx | 111 | ||||
-rw-r--r-- | subx/066print-byte.subx | 93 | ||||
-rw-r--r-- | subx/068error-byte.subx | 4 | ||||
-rw-r--r-- | subx/071read-line.subx | 40 | ||||
-rw-r--r-- | subx/072slice.subx | 24 | ||||
-rw-r--r-- | subx/Readme.md | 14 | ||||
-rwxr-xr-x | subx/apps/assort | bin | 21492 -> 21961 bytes | |||
-rw-r--r-- | subx/apps/assort.subx | 14 | ||||
-rwxr-xr-x | subx/apps/crenshaw2-1 | bin | 18651 -> 19120 bytes | |||
-rw-r--r-- | subx/apps/crenshaw2-1.subx | 4 | ||||
-rwxr-xr-x | subx/apps/crenshaw2-1b | bin | 19210 -> 19679 bytes | |||
-rw-r--r-- | subx/apps/crenshaw2-1b.subx | 7 | ||||
-rw-r--r-- | subx/apps/dquotes | bin | 21106 -> 23219 bytes | |||
-rw-r--r-- | subx/apps/dquotes.subx | 684 | ||||
-rwxr-xr-x | subx/apps/factorial | bin | 17567 -> 18036 bytes | |||
-rwxr-xr-x | subx/apps/handle | bin | 18394 -> 18863 bytes | |||
-rwxr-xr-x | subx/apps/hex | bin | 21660 -> 22129 bytes | |||
-rw-r--r-- | subx/apps/hex.subx | 18 | ||||
-rwxr-xr-x | subx/apps/pack | bin | 36252 -> 36721 bytes | |||
-rw-r--r-- | subx/apps/pack.subx | 72 | ||||
-rw-r--r-- | subx/apps/subx-common.subx | 6 |
22 files changed, 988 insertions, 165 deletions
diff --git a/subx/061read-byte.subx b/subx/061read-byte.subx index 7503818f..81080393 100644 --- a/subx/061read-byte.subx +++ b/subx/061read-byte.subx @@ -1,4 +1,4 @@ -# read-byte: one higher-level abstraction atop 'read'. +# 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 @@ -32,8 +32,8 @@ Stdin: # . 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: # run a single test, while debugging -#? e8/call test-read-byte-multiple/disp32 -#? e8/call test-read-byte-refills-buffer/disp32 +#? e8/call test-read-byte-buffered-multiple/disp32 +#? e8/call test-read-byte-buffered-refills-buffer/disp32 #? # syscall(exit, Num-test-failures) #? 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX #? b8/copy-to-EAX 1/imm32/exit @@ -41,7 +41,7 @@ Stdin: # return next byte value in EAX, with top 3 bytes cleared. # On reaching end of file, return 0xffffffff (Eof). -read-byte: # f : (address buffered-file) -> byte-or-Eof/EAX +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 @@ -54,7 +54,7 @@ read-byte: # f : (address buffered-file) -> byte-or-Eof/EAX 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:from-stream/disp8 + 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 @@ -75,17 +75,17 @@ read-byte: # f : (address buffered-file) -> byte-or-Eof/EAX 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:from-stream/disp8 + 75/jump-if-not-equal $read-byte-buffered:from-stream/disp8 b8/copy-to-EAX 0xffffffff/imm32/Eof - eb/jump $read-byte:end/disp8 -$read-byte:from-stream: + 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:end: +$read-byte-buffered:end: # . restore registers 5e/pop-to-ESI 59/pop-to-ECX @@ -96,8 +96,8 @@ $read-byte:end: # - tests -test-read-byte-single: - # - check that read-byte returns first byte of 'file' +test-read-byte-buffered-single: + # - check that read-byte-buffered returns first byte of 'file' # setup # . clear-stream(_test-stream) # . . push args @@ -123,16 +123,16 @@ test-read-byte-single: e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # read-byte(_test-buffered-file) + # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call - e8/call read-byte/disp32 + 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-single"/imm32 + 68/push "F - test-read-byte-buffered-single"/imm32 68/push 0x41/imm32 50/push-EAX # . . call @@ -142,8 +142,8 @@ test-read-byte-single: # . end c3/return -test-read-byte-multiple: - # - call read-byte twice, check that second call returns second byte +test-read-byte-buffered-multiple: + # - call read-byte-buffered twice, check that second call returns second byte # setup # . clear-stream(_test-stream) # . . push args @@ -169,23 +169,23 @@ test-read-byte-multiple: e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # read-byte(_test-buffered-file) + # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call - e8/call read-byte/disp32 + 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(_test-buffered-file) + # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call - e8/call read-byte/disp32 + 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-multiple"/imm32 + 68/push "F - test-read-byte-buffered-multiple"/imm32 68/push 0x62/imm32 50/push-EAX # . . call @@ -195,8 +195,8 @@ test-read-byte-multiple: # . end c3/return -test-read-byte-end-of-file: - # - call read-byte on an empty 'file', check that it returns Eof +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 @@ -214,16 +214,16 @@ test-read-byte-end-of-file: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # read-byte(_test-buffered-file) + # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call - e8/call read-byte/disp32 + 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-end-of-file"/imm32 + 68/push "F - test-read-byte-buffered-end-of-file"/imm32 68/push 0xffffffff/imm32/Eof 50/push-EAX # . . call @@ -233,8 +233,8 @@ test-read-byte-end-of-file: # . end c3/return -test-read-byte-refills-buffer: - # - consume buffered-file's buffer, check that next read-byte still works +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 @@ -264,16 +264,16 @@ test-read-byte-refills-buffer: # . _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(_test-buffered-file) + # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call - e8/call read-byte/disp32 + 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-refills-buffer"/imm32 + 68/push "F - test-read-byte-buffered-refills-buffer"/imm32 68/push 0x41/imm32 50/push-EAX # . . call diff --git a/subx/064write-byte.subx b/subx/064write-byte.subx index 9f97c336..606cc7d3 100644 --- a/subx/064write-byte.subx +++ b/subx/064write-byte.subx @@ -1,4 +1,4 @@ -# write-byte: write a single byte to a buffered-file. The write may be buffered. +# write-byte-buffered: add a single byte to a buffered-file. # flush: write out any buffered writes to disk. # # TODO: Come up with a way to signal failure to write to disk. This is hard @@ -28,7 +28,7 @@ Stdout: # . 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 # Write lower byte of 'n' to 'f'. -write-byte: # f : (address buffered-file), n : int -> <void> +write-byte-buffered: # f : (address buffered-file), n : int -> <void> # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP @@ -41,7 +41,7 @@ write-byte: # f : (address buffered-file), n : int -> <void> 8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 4/disp8 . # copy *(EDI+4) to ECX # if (f->write >= f->length) flush and clear f's stream 3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 0xc/disp8 . # compare ECX with *(EDI+12) - 7c/jump-if-lesser $write-byte:to-stream/disp8 + 7c/jump-if-lesser $write-byte-buffered:to-stream/disp8 # . flush(f) # . . push args 57/push-EDI @@ -59,7 +59,7 @@ write-byte: # f : (address buffered-file), n : int -> <void> 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . f->write must now be 0; update its cache at ECX 31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . . # clear ECX -$write-byte:to-stream: +$write-byte-buffered:to-stream: # write to stream # f->data[f->write] = LSB(n) 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX @@ -67,7 +67,7 @@ $write-byte:to-stream: 88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/EDI 1/index/ECX . 0/r32/AL 0x10/disp8 . # copy AL to *(EDI+ECX+16) # ++f->write ff 0/subop/increment 1/mod/*+disp8 7/rm32/EDI . . . . 4/disp8 . # increment *(EDI+4) -$write-byte:end: +$write-byte-buffered:end: # . restore registers 5f/pop-to-EDI 59/pop-to-ECX @@ -103,10 +103,8 @@ $flush:end: 5d/pop-to-EBP c3/return -# - tests - -test-write-byte-single: - # - check that write-byte writes to first byte of 'file' +test-write-byte-buffered-single: + # - check that write-byte-buffered writes to first byte of 'file' # setup # . clear-stream(_test-stream) # . . push args @@ -124,12 +122,12 @@ test-write-byte-single: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # write-byte(_test-buffered-file, 'A') + # write-byte-buffered(_test-buffered-file, 'A') # . . push args 68/push 0x41/imm32 68/push _test-buffered-file/imm32 # . . call - e8/call write-byte/disp32 + e8/call write-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # flush(_test-buffered-file) @@ -141,7 +139,7 @@ test-write-byte-single: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # check-stream-equal(_test-stream, "A", msg) # . . push args - 68/push "F - test-write-byte-single"/imm32 + 68/push "F - test-write-byte-buffered-single"/imm32 68/push "A"/imm32 68/push _test-stream/imm32 # . . call @@ -151,8 +149,8 @@ test-write-byte-single: # . end c3/return -test-write-byte-multiple-flushes: - # - check that write-byte correctly flushes buffered data +test-write-byte-buffered-multiple-flushes: + # - check that write-byte-buffered correctly flushes buffered data # setup # . clear-stream(_test-stream) # . . push args @@ -181,12 +179,12 @@ test-write-byte-multiple-flushes: e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # write-byte(_test-buffered-file, 'g') + # write-byte-buffered(_test-buffered-file, 'g') # . . push args 68/push 0x67/imm32 68/push _test-buffered-file/imm32 # . . call - e8/call write-byte/disp32 + e8/call write-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # flush(_test-buffered-file) @@ -198,7 +196,7 @@ test-write-byte-multiple-flushes: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # check-stream-equal(_test-stream, "abcdef", msg) # . . push args - 68/push "F - test-write-byte-multiple-flushes: 1"/imm32 + 68/push "F - test-write-byte-buffered-multiple-flushes: 1"/imm32 68/push "abcdefg"/imm32 68/push _test-stream/imm32 # . . call @@ -208,4 +206,83 @@ test-write-byte-multiple-flushes: # . end c3/return +# - variant without buffering + +# Write lower byte of 'n' to 'f'. +append-byte: # f : (address stream), n : int -> <void> + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 57/push-EDI + # EDI = f + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 8/disp8 . # copy *(EBP+8) to EDI + # ECX = f->write + 8b/copy 0/mod/indirect 7/rm32/EDI . . . 1/r32/ECX . . # copy *EDI to ECX + # if (f->write >= f->length) abort + 3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(EDI+8) + 7d/jump-if-greater-or-equal $append-byte:abort/disp8 +$append-byte:to-stream: + # write to stream + # f->data[f->write] = LSB(n) + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/AL 0xc/disp8 . # copy byte at *(EBP+12) to AL + 88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/EDI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy AL to *(EDI+ECX+12) + # ++f->write + ff 0/subop/increment 0/mod/indirect 7/rm32/EDI . . . . . . # increment *EDI +$append-byte:end: + # . restore registers + 5f/pop-to-EDI + 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 + +$append-byte:abort: + # . _write(2/stderr, error) + # . . push args + 68/push "append-byte: out of space"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-append-byte-single: + # - check that append-byte writes to 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 + # append-byte(_test-stream, 'A') + # . . push args + 68/push 0x41/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call append-byte/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-stream-equal(_test-stream, "A", msg) + # . . push args + 68/push "F - test-append-byte-single"/imm32 + 68/push "A"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . end + c3/return + # . . vim:nowrap:textwidth=0 diff --git a/subx/066print-byte.subx b/subx/066print-byte.subx index 39d7793e..80a29a0c 100644 --- a/subx/066print-byte.subx +++ b/subx/066print-byte.subx @@ -5,7 +5,7 @@ # . 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 -print-byte: # f : (address buffered-file), n : int -> <void> +append-byte-hex: # f : (address stream), n : int -> <void> # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP @@ -17,12 +17,12 @@ print-byte: # f : (address buffered-file), n : int -> <void> 25/and-EAX 0xf/imm32 # . AL = to-hex-char(AL) e8/call to-hex-char/disp32 - # write-byte(f, AL) + # append-byte(f, AL) # . . push args 50/push-EAX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call write-byte/disp32 + e8/call append-byte/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # AL = convert lower nibble to hex @@ -30,15 +30,15 @@ print-byte: # f : (address buffered-file), n : int -> <void> 25/and-EAX 0xf/imm32 # . AL = to-hex-char(AL) e8/call to-hex-char/disp32 - # write-byte(f, AL) + # append-byte(f, AL) # . . push args 50/push-EAX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call write-byte/disp32 + e8/call append-byte/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP -$print-byte:end: +$append-byte-hex:end: # . restore registers 58/pop-to-EAX # . epilog @@ -46,8 +46,79 @@ $print-byte:end: 5d/pop-to-EBP c3/return -test-print-byte: - # - check that print-byte prints the hex textual representation +test-append-byte-hex: + # - check that append-byte-hex adds the hex textual representation + # 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 + # append-byte-hex(_test-stream, 0xa) # exercises digit, non-digit as well as leading zero + # . . push args + 68/push 0xa/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call append-byte-hex/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-stream-equal(_test-stream, "0a", msg) + # . . push args + 68/push "F - test-append-byte-hex"/imm32 + 68/push "0a"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . end + c3/return + +print-byte-buffered: # f : (address buffered-file), n : int -> <void> + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + # AL = convert upper nibble to hex + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX + c1/shift 5/subop/logic-right 3/mod/direct 0/rm32/EAX . . . . . 4/imm8 # shift EAX right by 4 bits, while padding zeroes + 25/and-EAX 0xf/imm32 + # . AL = to-hex-char(AL) + e8/call to-hex-char/disp32 + # write-byte-buffered(f, AL) + # . . push args + 50/push-EAX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-byte-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # AL = convert lower nibble to hex + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX + 25/and-EAX 0xf/imm32 + # . AL = to-hex-char(AL) + e8/call to-hex-char/disp32 + # write-byte-buffered(f, AL) + # . . push args + 50/push-EAX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-byte-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$print-byte-buffered:end: + # . restore registers + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-print-byte-buffered: + # - check that print-byte-buffered prints the hex textual representation # setup # . clear-stream(_test-stream) # . . push args @@ -65,12 +136,12 @@ test-print-byte: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # print-byte(_test-buffered-file, 0xa) # exercises digit, non-digit as well as leading zero + # print-byte-buffered(_test-buffered-file, 0xa) # exercises digit, non-digit as well as leading zero # . . push args 68/push 0xa/imm32 68/push _test-buffered-file/imm32 # . . call - e8/call print-byte/disp32 + e8/call print-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # flush(_test-buffered-file) @@ -82,7 +153,7 @@ test-print-byte: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # check-stream-equal(_test-stream, "0a", msg) # . . push args - 68/push "F - test-print-byte"/imm32 + 68/push "F - test-print-byte-buffered"/imm32 68/push "0a"/imm32 68/push _test-stream/imm32 # . . call diff --git a/subx/068error-byte.subx b/subx/068error-byte.subx index 252aff20..dcffd026 100644 --- a/subx/068error-byte.subx +++ b/subx/068error-byte.subx @@ -52,12 +52,12 @@ error-byte: # ed : (address exit-descriptor), out : (address buffered-file), ms e8/call write-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # print-byte(out, byte) + # print-byte-buffered(out, byte) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call print-byte/disp32 + e8/call print-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # write-buffered(out, Newline) diff --git a/subx/071read-line.subx b/subx/071read-line.subx index 39c5f576..f33fd035 100644 --- a/subx/071read-line.subx +++ b/subx/071read-line.subx @@ -4,7 +4,7 @@ # . 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: # run a single test, while debugging -#? e8/call test-read-line/disp32 +#? e8/call test-read-line-buffered/disp32 #? # syscall(exit, Num-test-failures) #? 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX #? b8/copy-to-EAX 1/imm32/exit @@ -13,7 +13,7 @@ # read bytes from 'f' until (and including) a newline and store them into 's' # 's' fails to grow if and only if no data found # just abort if 's' is too small -read-line: # f : (address buffered-file), s : (address stream byte) -> <void> +read-line-buffered: # f : (address buffered-file), s : (address stream byte) -> <void> # pseudocode: # while true # if (s->write >= s->length) abort @@ -41,13 +41,13 @@ read-line: # f : (address buffered-file), s : (address stream byte) -> <void> 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0xc/disp8 . # copy *(EBP+12) to EDI # EDX = s->write 8b/copy 0/mod/indirect 7/rm32/EDI . . . 2/r32/EDX . . # copy *EDI to EDX -$read-line:loop: +$read-line-buffered:loop: # if (s->write >= s->length) abort 3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 2/r32/EDX 8/disp8 . # compare EDX with *(EDI+8) - 7d/jump-if-greater-or-equal $read-line:abort/disp8 + 7d/jump-if-greater-or-equal $read-line-buffered:abort/disp8 # 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-line:from-stream/disp8 + 7c/jump-if-lesser $read-line-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 @@ -70,8 +70,8 @@ $read-line:loop: # since f->read was initially 0, EAX is the same as f->write # . if (EAX == 0) return true 3d/compare-EAX-and 0/imm32 - 74/jump-if-equal $read-line:end/disp8 -$read-line:from-stream: + 74/jump-if-equal $read-line-buffered:end/disp8 +$read-line-buffered: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 @@ -83,8 +83,8 @@ $read-line:from-stream: 42/increment-EDX # if (AL == '\n') return 3d/compare-EAX-and 0xa/imm32 - 75/jump-if-not-equal $read-line:loop/disp8 -$read-line:end: + 75/jump-if-not-equal $read-line-buffered:loop/disp8 +$read-line-buffered:end: # save f->read 89/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # copy ECX to *(ESI+8) # save s->write @@ -100,10 +100,10 @@ $read-line:end: 5d/pop-to-EBP c3/return -$read-line:abort: +$read-line-buffered:abort: # . _write(2/stderr, error) # . . push args - 68/push "read-line: line too long\n"/imm32 + 68/push "read-line-buffered: line too long\n"/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 @@ -115,8 +115,8 @@ $read-line:abort: cd/syscall 0x80/imm8 # never gets here -test-read-line: - # - check that read-line stops at a newline +test-read-line-buffered: + # - check that read-line-buffered stops at a newline # setup # . clear-stream(_test-stream) # . . push args @@ -150,17 +150,17 @@ test-read-line: # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream - # . EAX = read-line(_test-buffered-file, _test-tmp-stream) + # . EAX = read-line-buffered(_test-buffered-file, _test-tmp-stream) # . . push args 68/push _test-tmp-stream/imm32 68/push _test-buffered-file/imm32 # . . call - e8/call read-line/disp32 + e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # check-next-stream-line-equal(_test-tmp-stream, "ab", msg) # . . push args - 68/push "F - test-read-line"/imm32 + 68/push "F - test-read-line-buffered"/imm32 68/push "ab"/imm32 68/push _test-tmp-stream/imm32 # . . call @@ -170,7 +170,7 @@ test-read-line: # end c3/return -test-read-line-reads-final-line-until-Eof: +test-read-line-buffered-reads-final-line-until-Eof: # setup # . clear-stream(_test-stream) # . . push args @@ -204,17 +204,17 @@ test-read-line-reads-final-line-until-Eof: # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream - # . EAX = read-line(_test-buffered-file, _test-tmp-stream) + # . EAX = read-line-buffered(_test-buffered-file, _test-tmp-stream) # . . push args 68/push _test-tmp-stream/imm32 68/push _test-buffered-file/imm32 # . . call - e8/call read-line/disp32 + e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # check-stream-equal(_test-tmp-stream, "cd", msg) # . . push args - 68/push "F - test-read-line-reads-final-line-until-Eof"/imm32 + 68/push "F - test-read-line-buffered-reads-final-line-until-Eof"/imm32 68/push "cd"/imm32 68/push _test-tmp-stream/imm32 # . . call diff --git a/subx/072slice.subx b/subx/072slice.subx index 683c4c7b..82ce883e 100644 --- a/subx/072slice.subx +++ b/subx/072slice.subx @@ -704,7 +704,7 @@ test-slice-starts-with-fails-2: 5d/pop-to-EBP c3/return -write-slice: # out : (address buffered-file), s : (address slice) +write-slice-buffered: # out : (address buffered-file), s : (address slice) # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP @@ -727,13 +727,13 @@ write-slice: # out : (address buffered-file), s : (address slice) 8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 2/r32/EDX 0xc/disp8 . # copy *(EDI+12) to EDX # EBX = out->write 8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 3/r32/EBX 4/disp8 . # copy *(EDI+4) to EBX -$write-slice:loop: +$write-slice-buffered:loop: # if (curr >= max) break 39/compare 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # compare ECX with ESI - 7d/jump-if-greater-or-equal $write-slice:loop-end/disp8 + 7d/jump-if-greater-or-equal $write-slice-buffered:loop-end/disp8 # if (out->write >= out->length) flush and clear out's stream 39/compare 3/mod/direct 3/rm32/EBX . . . 2/r32/EDX . . # compare EBX with EDX - 7c/jump-if-lesser $write-slice:to-stream/disp8 + 7c/jump-if-lesser $write-slice-buffered:to-stream/disp8 # . persist out->write 89/copy 1/mod/*+disp8 7/rm32/EDI . . . 3/r32/EBX 4/disp8 . # copy EBX to *(EDI+4) # . flush(out) @@ -753,7 +753,7 @@ $write-slice:loop: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . out->write must now be 0; update its cache at EBX 31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX -$write-slice:to-stream: +$write-slice-buffered:to-stream: # out->data[out->write] = *in # . AL = *in 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX @@ -764,11 +764,11 @@ $write-slice:to-stream: 43/increment-EBX # ++in 41/increment-ECX - eb/jump $write-slice:loop/disp8 -$write-slice:loop-end: + eb/jump $write-slice-buffered:loop/disp8 +$write-slice-buffered:loop-end: # persist necessary variables from registers 89/copy 1/mod/*+disp8 7/rm32/EDI . . . 3/r32/EBX 4/disp8 . # copy EBX to *(EDI+4) -$write-slice:end: +$write-slice-buffered:end: # . restore registers 5f/pop-to-EDI 5e/pop-to-ESI @@ -781,7 +781,7 @@ $write-slice:end: 5d/pop-to-EBP c3/return -test-write-slice: +test-write-slice-buffered: # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP @@ -806,12 +806,12 @@ test-write-slice: 68/push _test-slice-data-3/imm32/end 68/push _test-slice-data-0/imm32/start 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX - # write-slice(_test-buffered-file, slice) + # write-slice-buffered(_test-buffered-file, slice) # . . push args 51/push-ECX 68/push _test-buffered-file/imm32 # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # flush(_test-buffered-file) @@ -823,7 +823,7 @@ test-write-slice: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # check-stream-equal(_test-stream, "Abc", msg) # . . push args - 68/push "F - test-write-slice"/imm32 + 68/push "F - test-write-slice-buffered"/imm32 68/push "Abc"/imm32 68/push _test-stream/imm32 # . . call diff --git a/subx/Readme.md b/subx/Readme.md index e8e4ad6d..517515d8 100644 --- a/subx/Readme.md +++ b/subx/Readme.md @@ -626,17 +626,23 @@ allocated memory for it.)_ * `write-stream`: stream -> file - Can also be used to cat one stream into another. - Will abort the entire program if there isn't enough room. +* `append-byte`: int -> stream + - Will abort the entire program if there isn't enough room. +* `append-byte-hex`: int -> stream + - Will abort the entire program if there isn't enough room. * `write-buffered`: string -> buffered-file -* `write-slice`: slice -> buffered-file +* `write-slice-buffered`: slice -> buffered-file * `flush`: buffered-file -* `print-byte`: buffered-file, int +* `write-byte-buffered`: int -> buffered-file +* `print-byte-buffered`: int -> buffered-file + - textual representation in hex #### reading from disk * `read`: file -> stream - Can also be used to cat one stream into another. - Will silently stop reading when destination runs out of space. -* `read-byte`: buffered-file -> byte -* `read-line`: buffered-file -> stream +* `read-byte-buffered`: buffered-file -> byte +* `read-line-buffered`: buffered-file -> stream - Will abort the entire program if there isn't enough room. #### non-IO operations on streams diff --git a/subx/apps/assort b/subx/apps/assort index e331ce8a..fb24828b 100755 --- a/subx/apps/assort +++ b/subx/apps/assort Binary files differdiff --git a/subx/apps/assort.subx b/subx/apps/assort.subx index cfdef5e1..7498cc3a 100644 --- a/subx/apps/assort.subx +++ b/subx/apps/assort.subx @@ -446,7 +446,7 @@ read-segments: # in : (address buffered-file), table : (address stream row) # var line = new-stream(512, 1) # while true # clear-stream(line) - # read-line(in, line) + # read-line-buffered(in, line) # if (line->write == 0) break # end of file # var word-slice = next-word(line) # if slice-empty?(word-slice) # whitespace @@ -497,12 +497,12 @@ $read-segments:loop: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # read-line(in, line) + # read-line-buffered(in, line) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-line/disp32 + e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $read-segments:check0: @@ -584,12 +584,12 @@ $read-segments:check-for-segment-header: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 52/push-EDX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -648,12 +648,12 @@ $read-segments:check-for-segment-header: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 52/push-EDX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) diff --git a/subx/apps/crenshaw2-1 b/subx/apps/crenshaw2-1 index eb187bb3..2ffc8a84 100755 --- a/subx/apps/crenshaw2-1 +++ b/subx/apps/crenshaw2-1 Binary files differdiff --git a/subx/apps/crenshaw2-1.subx b/subx/apps/crenshaw2-1.subx index 20560288..2f1fec9e 100644 --- a/subx/apps/crenshaw2-1.subx +++ b/subx/apps/crenshaw2-1.subx @@ -512,11 +512,11 @@ get-char: # f : (address buffered-file) -> <void> 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers 50/push-EAX - # read-byte(f) + # EAX = read-byte-buffered(f) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-byte/disp32 + e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # save EAX to Look diff --git a/subx/apps/crenshaw2-1b b/subx/apps/crenshaw2-1b index e8d72937..6144d591 100755 --- a/subx/apps/crenshaw2-1b +++ b/subx/apps/crenshaw2-1b Binary files differdiff --git a/subx/apps/crenshaw2-1b.subx b/subx/apps/crenshaw2-1b.subx index 2c396fb3..8dee0dbc 100644 --- a/subx/apps/crenshaw2-1b.subx +++ b/subx/apps/crenshaw2-1b.subx @@ -195,7 +195,8 @@ get-num: # in : (address buffered-file), out : (address stream), err : fd or (a # Look = get-char(in) # while is-digit?(Look) # This is complicated because I don't want to hard-code the error strategy in - # a general helper like write-byte. Maybe I should just create a local helper. + # a general helper like write-byte-buffered. Maybe I should just create a + # local helper. # # within the loop we'll try to keep things in registers: # in: ESI @@ -711,11 +712,11 @@ get-char: # f : (address buffered-file) -> <void> 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers 50/push-EAX - # read-byte(f) + # EAX = read-byte-buffered(f) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-byte/disp32 + e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # save EAX to Look diff --git a/subx/apps/dquotes b/subx/apps/dquotes index b63744d1..a91f829d 100644 --- a/subx/apps/dquotes +++ b/subx/apps/dquotes Binary files differdiff --git a/subx/apps/dquotes.subx b/subx/apps/dquotes.subx index fcac2698..e6b24698 100644 --- a/subx/apps/dquotes.subx +++ b/subx/apps/dquotes.subx @@ -31,7 +31,7 @@ Entry: # run tests if necessary, convert stdin if not #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . test() -#? e8/call test-next-word-returns-string-with-escapes/disp32 +#? e8/call test-emit-string-literal-data/disp32 #? 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX #? eb/jump $main:end/disp8 @@ -89,6 +89,10 @@ $main:end: b8/copy-to-EAX 1/imm32/exit cd/syscall 0x80/imm8 +# conceptual hierarchy within a line: +# line = words separated by ' ', maybe followed by comment starting with '#' +# word = datum until '/', then 0 or more metadata separated by '/' + convert: # in : (address buffered-file), out : (address buffered-file) -> <void> # pseudocode: # var line = new-stream(512, 1) @@ -96,7 +100,7 @@ convert: # in : (address buffered-file), out : (address buffered-file) -> <void # write-stream(new-data-segment, "== data\n") # while true # clear-stream(line) - # read-line(in, line) + # read-line-buffered(in, line) # if (line->write == 0) break # end of file # while true # var word-slice = next-word(line) @@ -107,7 +111,7 @@ convert: # in : (address buffered-file), out : (address buffered-file) -> <void # if slice-starts-with?(word-slice, '"') # string literal <== what we're here for # process-string-literal(word-slice, out, new-data-segment) # else - # write-slice(out, word-slice) + # write-slice-buffered(out, word-slice) # write-stream-data(out, new-data-segment) # flush(out) # @@ -159,12 +163,12 @@ $convert:line-loop: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # read-line(in, line) + # read-line-buffered(in, line) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-line/disp32 + e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $convert:check0: @@ -218,12 +222,12 @@ $convert:string-literal: # continue eb/jump $convert:next-word/disp8 $convert:regular-word: - # write-slice(out, word-slice) + # write-slice-buffered(out, word-slice) # . . push args 52/push-EDX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # fall through @@ -296,6 +300,13 @@ process-string-literal: # string-literal : (address slice), out : (address buff # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . print-int32-decimal(out-segment, *Next-string-literal) + # . . push args + ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Next-string-literal/disp32 # push *Next-string-literal + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) + # . . call + e8/call print-int32-decimal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . write(out-segment, ":\n") # . . push args 68/push ":\n"/imm32 @@ -322,8 +333,30 @@ process-string-literal: # string-literal : (address slice), out : (address buff e8/call write-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . print-int32-decimal(tmp, *Next-string-literal) + # . print-int32-decimal(int32-stream, *Next-string-literal) + # . . push args + ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Next-string-literal/disp32 # push *Next-string-literal + 51/push-ECX + # . . call + e8/call print-int32-decimal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-stream-data(out, int32-stream) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-stream-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # emit-metadata(out, string-literal) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call emit-metadata/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # ++ *Next-string-literal ff 0/subop/increment 0/mod/indirect 5/rm32/.disp32 . . . Next-string-literal/disp32 # increment *Num-test-failures $process-string-literal:end: @@ -593,6 +626,611 @@ test-convert-is-idempotent-by-default: 5d/pop-to-EBP c3/return +test-convert-processes-string-literals: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-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-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-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 + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-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-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-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 + # initialize input (meta comments in parens) + # == code (new segment) + # 1 "a"/x + # 2 "bc"/y + 68/push "== code\n"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(_test-input-stream, "1 \"a\"/x\n") + # . . push args + 68/push "1 \"a\"/x\n"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(_test-input-stream, "2 \"bc\"/y\n") + # . . push args + 68/push "2 \"bc\"/y\n"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check output + # == code + # 1 _string1/x + # 2 _string2/y + # == data + # _string1: + # 1/imm32 61/a + # _string2: + # 2/imm32 62/b 63/c + # We don't care right now what exactly happens to comments. Trailing spaces are also minor details. + # + # Open question: how to make this check more robust. + # We don't actually care what the auto-generated string variables are + # called. We just want to make sure instructions using string literals + # switch to a string variable with the right value. + # (Modifying string literals completely off the radar for now.) +#? # dump output {{{ +#? # . write(2/stderr, "result: ^") +#? # . . push args +#? 68/push "result: ^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-next-stream-line-equal(_test-output-stream, "== code ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/0"/imm32 + 68/push "== code "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "1 _string1/x ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/1"/imm32 + 68/push "1 _string1/x "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "2 _string2/y ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/2"/imm32 + 68/push "2 3 "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "== data ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/3"/imm32 + 68/push "== data "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "_string1: ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/4"/imm32 + 68/push "_string1: "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "1/imm32 61/a ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/5"/imm32 + 68/push "1/imm32 61/a "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "_string2: ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/6"/imm32 + 68/push "_string2: "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . check-next-stream-line-equal(_test-output-stream, "2/imm32 62/b 63/c ", msg) + # . . push args + 68/push "F - test-convert-processes-string-literals/7"/imm32 + 68/push "2/imm32 62/b 63/c "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-next-stream-line-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# generate the data segment contents byte by byte for a given slice +emit-string-literal-data: # out : (address stream), word : (address slice) + # pseudocode + # curr = word->start + # ++curr # skip '"' + # while true + # if (curr >= word->end) break + # c = *curr + # if (c == '"') break + # append-byte-hex(out, c) + # if c is alphanumeric: + # write(out, "/") + # append-byte(out, c) + # write(out, " ") + # ++curr + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = word + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI + # curr/EDX = word->start + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + # ++curr # skip initial '"' + 42/increment-EDX + # max/ESI = word->end + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 6/r32/ESI 4/disp8 . # copy *(ESI+4) to ESI + # ECX = 0 + 31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . . # clear ECX +$emit-string-literal-data:loop: + # if (curr >= max) break + 39/compare 3/mod/direct 2/rm32/EDX . . . 6/r32/ESI . . # compare EDX with ESI + 7d/jump-if-greater-or-equal $emit-string-literal-data:end/disp8 + # CL = *curr + 8a/copy-byte 0/mod/indirect 2/rm32/EDX . . . 1/r32/CL . . # copy byte at *EDX to CL + # if (ECX == '"') break + 81 7/subop/compare 3/mod/direct 1/rm32/ECX . . . . . 0x22/imm32/dquote # compare ECX + 74/jump-if-equal $emit-string-literal-data:end/disp8 + # append-byte-hex(out, CL) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call append-byte-hex/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # if (is-alphanumeric?(*curr)) print(out, "/#{*curr}") + # . EAX = is-alphanumeric?(CL) + # . . push args + 51/push-ECX + # . . call + e8/call is-alphanumeric?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . if (EAX == 0) goto char-done + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $emit-string-literal-data:char-done/disp8 + # . write(out, "/") + # . . push args + 68/push Slash/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . append-byte(out, *curr) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call append-byte/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$emit-string-literal-data:char-done: + # write(out, " ") + # . . push args + 68/push Space/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # ++curr + 42/increment-EDX + eb/jump $emit-string-literal-data:loop/disp8 +$emit-string-literal-data:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +is-alphanumeric?: # c : int -> EAX : boolean + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # EAX = c + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX + # if (EAX < '0') return false + 3d/compare-EAX-with 0x30/imm32/0 + 7c/jump-if-lesser $is-alphanumeric?:false/disp8 + # if (EAX <= '9') return true + 3d/compare-EAX-with 0x39/imm32/9 + 7e/jump-if-lesser-or-equal $is-alphanumeric?:true/disp8 + # if (EAX < 'A') return false + 3d/compare-EAX-with 0x41/imm32/A + 7c/jump-if-lesser $is-alphanumeric?:false/disp8 + # if (EAX <= 'Z') return true + 3d/compare-EAX-with 0x5a/imm32/Z + 7e/jump-if-lesser-or-equal $is-alphanumeric?:true/disp8 + # if (EAX < 'a') return false + 3d/compare-EAX-with 0x61/imm32/a + 7c/jump-if-lesser $is-alphanumeric?:false/disp8 + # if (EAX <= 'z') return true + 3d/compare-EAX-with 0x7a/imm32/z + 7e/jump-if-lesser-or-equal $is-alphanumeric?:true/disp8 + # return false +$is-alphanumeric?:false: + b8/copy-to-EAX 0/imm32/false + eb/jump $is-alphanumeric?:end/disp8 +$is-alphanumeric?:true: + b8/copy-to-EAX 1/imm32/true +$is-alphanumeric?:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-string-literal-data: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-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 + # var slice/ECX = '"abc"' + 68/push _test-slice-abc-end/imm32 + 68/push _test-slice-abc/imm32 + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-string-literal-data(_test-output-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-output-stream/imm32 + # . . call + e8/call emit-string-literal-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "result: ^") +#? # . . push args +#? 68/push "result: ^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "61/a 62/b 63/c ", msg) + # . . push args + 68/push "F - test-emit-string-literal-data"/imm32 + 68/push "61/a 62/b 63/c "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-string-literal-data-empty: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-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 + # var slice/ECX = '""' + 68/push _test-slice-empty-string-literal-end/imm32 + 68/push _test-slice-empty-string-literal/imm32 + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-string-literal-data(_test-output-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-output-stream/imm32 + # . . call + e8/call emit-string-literal-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "result: ^") +#? # . . push args +#? 68/push "result: ^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "", msg) + # . . push args + 68/push "F - test-emit-string-literal-data-empty"/imm32 + 68/push ""/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# just to keep things simple +test-emit-string-literal-data-no-metadata-for-non-alphanumerics: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-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 + # var slice/ECX = '"a b"' + 68/push _test-slice-a-space-b-end/imm32 + 68/push _test-slice-a-space-b/imm32 + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-string-literal-data(_test-output-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-output-stream/imm32 + # . . call + e8/call emit-string-literal-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "result: ^") +#? # . . push args +#? 68/push "result: ^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "61/a 20 62/b ", msg) # ideally we'd like to say '20/space' but that requires managing names for codepoints + # . . push args + 68/push "F - test-emit-string-literal-data-no-metadata-for-non-alphanumerics"/imm32 + 68/push "61/a 20 62/b "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-string-literal-data-handles-escape-sequences: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-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 + # var slice/ECX = '"a\"b"' + 68/push _test-slice-a-dquote-b-end/imm32 + 68/push _test-slice-a-dquote-b/imm32 + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-string-literal-data(_test-output-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-output-stream/imm32 + # . . call + e8/call emit-string-literal-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "result: ^") +#? # . . push args +#? 68/push "result: ^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "61/a 22 62/b ", msg) + # . . push args + 68/push "F - test-emit-string-literal-data-handles-escape-sequences"/imm32 + 68/push "61/a 22 62/b "/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# emit everything from a word except the initial datum +emit-metadata: # out : (address buffered-file), word : (address slice) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers +$emit-metadata:end: + # . reclaim locals + # . restore registers + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + # (re)compute the bounds of the next word in the line # return empty string on reaching end of file next-word: # line : (address stream byte), out : (address slice) @@ -1078,4 +1716,34 @@ Heap: # limit 0/imm32 +# length-prefixed string containing just a single space +Space: + # size + 1/imm32 + # data + 20/space + +# length-prefixed string containing just a single slash +Slash: + # size + 1/imm32 + # data + 2f/slash + +_test-slice-abc: + 22/dquote 61/a 62/b 63/c 22/dquote # "abc" +_test-slice-abc-end: + +_test-slice-empty-string-literal: + 22/dquote 22/dquote # "" +_test-slice-empty-string-literal-end: + +_test-slice-a-space-b: + 22/dquote 61/a 20/space 62/b 22/dquote # "a b" +_test-slice-a-space-b-end: + +_test-slice-a-dquote-b: + 22/dquote 61/a 5c/backslash 22/dquote 62/b 22/dquote # "a\"b" +_test-slice-a-dquote-b-end: + # . . vim:nowrap:textwidth=0 diff --git a/subx/apps/factorial b/subx/apps/factorial index 2c47ab8a..d1bd5f17 100755 --- a/subx/apps/factorial +++ b/subx/apps/factorial Binary files differdiff --git a/subx/apps/handle b/subx/apps/handle index 274677fe..b77ebba6 100755 --- a/subx/apps/handle +++ b/subx/apps/handle Binary files differdiff --git a/subx/apps/hex b/subx/apps/hex index 70734450..dde157e3 100755 --- a/subx/apps/hex +++ b/subx/apps/hex Binary files differdiff --git a/subx/apps/hex.subx b/subx/apps/hex.subx index c0627c77..3730351f 100644 --- a/subx/apps/hex.subx +++ b/subx/apps/hex.subx @@ -75,7 +75,7 @@ convert: # in : (address buffered-file), out : (address buffered-file), err : ( # while true # EAX = convert-next-octet(in, err, ed) # if (EAX == Eof) break - # write-byte(out, AL) + # write-byte-buffered(out, AL) # flush(out) # # . prolog @@ -96,12 +96,12 @@ $convert:loop: # if (EAX == Eof) break 3d/compare-EAX-and 0xffffffff/imm32/Eof 74/jump-if-equal $convert:loop-end/disp8 - # write-byte(out, AL) + # write-byte-buffered(out, AL) # . . push args 50/push-EAX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-byte/disp32 + e8/call write-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # loop @@ -489,7 +489,7 @@ $test-convert-next-octet-aborts-on-single-hex-byte:end: scan-next-byte: # in : (address buffered-file), err : (address buffered-file), ed : (address exit-descriptor) -> byte-or-Eof/EAX # pseudocode: # while true - # EAX = read-byte(in) + # EAX = read-byte-buffered(in) # if (EAX == Eof) return EAX # if (is-hex-digit?(EAX)) return EAX # if (EAX == ' ' or '\t' or '\n') continue @@ -501,11 +501,11 @@ scan-next-byte: # in : (address buffered-file), err : (address buffered-file), 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers $scan-next-byte:loop: - # EAX = read-byte(in) + # EAX = read-byte-buffered(in) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-byte/disp32 + e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # if (EAX == Eof) return EAX @@ -1389,7 +1389,7 @@ skip-until-newline: # in : (address buffered-file) -> <void> # pseudocode: # push EAX # while true - # EAX = read-byte(in) + # EAX = read-byte-buffered(in) # if (EAX == Eof) break # if (EAX == 0x0a) break # pop EAX @@ -1399,11 +1399,11 @@ skip-until-newline: # in : (address buffered-file) -> <void> # . save registers 50/push-EAX $skip-until-newline:loop: - # . EAX = read-byte(in) + # . EAX = read-byte-buffered(in) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-byte/disp32 + e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . if (EAX == Eof) break diff --git a/subx/apps/pack b/subx/apps/pack index 7e8cc761..1154be78 100755 --- a/subx/apps/pack +++ b/subx/apps/pack Binary files differdiff --git a/subx/apps/pack.subx b/subx/apps/pack.subx index 6ac03fc4..88ed23b6 100644 --- a/subx/apps/pack.subx +++ b/subx/apps/pack.subx @@ -96,7 +96,7 @@ convert: # in : (address buffered-file), out : (address buffered-file) -> <void # var in-code? = false # while true # clear-stream(line) - # read-line(in, line) + # read-line-buffered(in, line) # if (line->write == 0) break # end of file # var word-slice = next-word(line) # if slice-empty?(word-slice) # whitespace @@ -141,12 +141,12 @@ $convert:loop: e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # read-line(in, line) + # read-line-buffered(in, line) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call read-line/disp32 + e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $convert:check0: @@ -218,12 +218,12 @@ $convert:check2: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 52/push-EDX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -283,12 +283,12 @@ $convert:check2: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 52/push-EDX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -937,7 +937,7 @@ convert-data: # line : (address stream byte), out : (address buffered-file) -> # if slice-empty?(word-slice) # end of file (maybe including trailing whitespace) # break # skip emitting some whitespace # if slice-starts-with?(word-slice, "#") # comment - # write-slice(out, word-slice) + # write-slice-buffered(out, word-slice) # break # if slice-ends-with?(word-slice, ":") # label # write-stream-data(out, line) @@ -1012,12 +1012,12 @@ $convert-data:loop: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 51/push-ECX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -1059,12 +1059,12 @@ $convert-data:check-for-comment: 3d/compare-EAX-and 0x23/imm32/hash 75/jump-if-not-equal $convert-data:check-for-label/disp8 $convert-data:comment: - # write-slice(out, word-slice) + # write-slice-buffered(out, word-slice) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # break @@ -1999,14 +1999,14 @@ emit-opcodes: # line : (address stream byte), out : (address buffered-file) -> # var op1 = next-word(line) # if (slice-empty?(op1) || slice-starts-with?(op1, "#")) return # op1 = next-token-from-slice(op1->start, op1->end, "/") - # write-slice(out, op1) + # write-slice-buffered(out, op1) # if !slice-equal?(op1, "0f") && !slice-equal?(op1, "f2") && !slice-equal?(op1, "f3") # return # # var op2 = next-word(line) # if (slice-empty?(op2) || slice-starts-with?(op2, "#")) return # op2 = next-token-from-slice(op2->start, op2->end, "/") - # write-slice(out, op2) + # write-slice-buffered(out, op2) # if slice-equal?(op1, "0f") # return # if !slice-equal?(op2, "0f") @@ -2015,7 +2015,7 @@ emit-opcodes: # line : (address stream byte), out : (address buffered-file) -> # var op3 = next-word(line) # if (slice-empty?(op3) || slice-starts-with?(op3, "#")) return # op3 = next-token-from-slice(op3->start, op3->end, "/") - # write-slice(out, op3) + # write-slice-buffered(out, op3) # # . prolog 55/push-EBP @@ -2079,12 +2079,12 @@ $emit-opcodes:op1: e8/call next-token-from-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP - # write-slice(out, op1) + # write-slice-buffered(out, op1) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # write-buffered(out, " ") @@ -2172,12 +2172,12 @@ $emit-opcodes:op2: e8/call next-token-from-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP - # write-slice(out, op2) + # write-slice-buffered(out, op2) # . . push args 52/push-EDX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # write-buffered(out, " ") @@ -2251,12 +2251,12 @@ $emit-opcodes:op3: e8/call next-token-from-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP - # write-slice(out, op3) + # write-slice-buffered(out, op3) # . . push args 52/push-EDX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # write-buffered(out, " ") @@ -2388,12 +2388,12 @@ $emit-modrm:loop: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 51/push-ECX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -2699,12 +2699,12 @@ $emit-sib:loop: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 51/push-ECX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -2964,12 +2964,12 @@ $emit-disp:loop: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 51/push-ECX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -3185,12 +3185,12 @@ $emit-imm:loop: #? e8/call clear-stream/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP -#? # . write-slice(Stderr, word-slice) +#? # . write-slice-buffered(Stderr, word-slice) #? # . . push args #? 51/push-ECX #? 68/push Stderr/imm32 #? # . . call -#? e8/call write-slice/disp32 +#? e8/call write-slice-buffered/disp32 #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # . flush(Stderr) @@ -6373,7 +6373,7 @@ emit: # out : (address buffered-file), word : (address slice), width : int -> < e8/call next-token-from-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP - # if (is-valid-name?(datum)) write-slice(out, word) and return + # if (is-valid-name?(datum)) write-slice-buffered(out, word) and return # . EAX = is-valid-name?(name) # . . push args 57/push-EDI @@ -6385,12 +6385,12 @@ emit: # out : (address buffered-file), word : (address slice), width : int -> < 3d/compare-EAX-and 0/imm32 74/jump-if-equal $emit:hex-int/disp8 $emit:name: - # . write-slice(out, word) + # . write-slice-buffered(out, word) # . . push args 56/push-ESI ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . write-buffered(out, " ") @@ -7054,20 +7054,20 @@ $emit-hex:loop: # if (curr >= width) break 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX 7d/jump-if-greater-or-equal $emit-hex:end/disp8 - # print-byte(out, EBX) + # print-byte-buffered(out, EBX) # . . push args 53/push-EBX 57/push-EDI # . . call - e8/call print-byte/disp32 + e8/call print-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # write-byte(out, ' ') + # write-byte-buffered(out, ' ') # . . push args 68/push 0x20/imm32/space 57/push-EDI # . . call - e8/call write-byte/disp32 + e8/call write-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # EBX = EBX >> 8 diff --git a/subx/apps/subx-common.subx b/subx/apps/subx-common.subx index 0807b210..cba1e9cc 100644 --- a/subx/apps/subx-common.subx +++ b/subx/apps/subx-common.subx @@ -7,7 +7,7 @@ # write an entire stream's contents to a buffered-file # ways to do this: -# - construct a 'maximal slice' and pass it to write-slice +# - construct a 'maximal slice' and pass it to write-slice-buffered # - flush the buffered-file and pass the stream directly to its fd (disabling buffering) # we'll go with the first way for now write-stream-data: # f : (address buffered-file), s : (address stream) -> <void> @@ -30,12 +30,12 @@ write-stream-data: # f : (address buffered-file), s : (address stream) -> <void 50/push-EAX # . ECX = ESP 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX - # write-slice(f, slice) + # write-slice-buffered(f, slice) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call - e8/call write-slice/disp32 + e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $write-stream-data:end: |